Remove string using certain values (-)

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I have following text




foo-11.11-fo.o-foo-bar



bar-foo-11.11-22.11




I want to remove last strings that have two "-".



Expected output:




foo-11.11-fo.o



bar-foo




I have tried multiple methods using cut, but nothing works.










share|improve this question



























    up vote
    0
    down vote

    favorite












    I have following text




    foo-11.11-fo.o-foo-bar



    bar-foo-11.11-22.11




    I want to remove last strings that have two "-".



    Expected output:




    foo-11.11-fo.o



    bar-foo




    I have tried multiple methods using cut, but nothing works.










    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have following text




      foo-11.11-fo.o-foo-bar



      bar-foo-11.11-22.11




      I want to remove last strings that have two "-".



      Expected output:




      foo-11.11-fo.o



      bar-foo




      I have tried multiple methods using cut, but nothing works.










      share|improve this question















      I have following text




      foo-11.11-fo.o-foo-bar



      bar-foo-11.11-22.11




      I want to remove last strings that have two "-".



      Expected output:




      foo-11.11-fo.o



      bar-foo




      I have tried multiple methods using cut, but nothing works.







      shell-script text-processing sed scripting cut






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 25 '17 at 12:51









      Jeff Schaller

      32.4k849110




      32.4k849110










      asked Sep 25 '17 at 11:52









      Buvanesh Kumar

      106110




      106110




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          $ cat ip.txt 
          foo-11.11-fo.o-foo-bar
          bar-foo-11.11-22.11

          $ rev ip.txt
          rab-oof-o.of-11.11-oof
          11.22-11.11-oof-rab

          $ rev ip.txt | cut -d- -f3- | rev
          foo-11.11-fo.o
          bar-foo


          Reverse each line, then use cut to select all fields except first two and then reverse the output again





          You can also use perl, but would print empty lines if any input line has less than 3 fields



          $ perl -F'-' -lane 'print join "-", @F[0..$#F-2]' ip.txt 
          foo-11.11-fo.o
          bar-foo


          Specify - as input delimiter and then print all but last two fields






          share|improve this answer




















          • rev that's what I missed. thanks a lot, working great.
            – Buvanesh Kumar
            Sep 25 '17 at 12:18

















          up vote
          4
          down vote













          Using sed:



          $ sed 's/-[^-]*-[^-]*$//' file
          foo-11.11-fo.o
          bar-foo


          This will remove -X-X at the end of every line in file, where X is any string that does not include a -.



          If the strings are in a shell variable:



          $ s='foo-11.11-fo.o-foo-bar'
          $ printf '%sn' "$s%-*-*"
          foo-11.11-fo.o

          $ s='bar-foo-11.11-22.11'
          $ printf '%sn' "$s%-*-*"
          bar-foo


          or in a bash array:



          $ s=( 'foo-11.11-fo.o-foo-bar' 'bar-foo-11.11-22.11' )
          $ printf '%sn' "$s[@]%-*-*"
          foo-11.11-fo.o
          bar-foo


          This removes anything matching the pattern -*-* at the end of the string in the variable s through a suffix pattern match/removal. In the case where s in an array, the removal is done on all elements of the array.






          share|improve this answer






















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f394305%2fremove-string-using-certain-values%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted










            $ cat ip.txt 
            foo-11.11-fo.o-foo-bar
            bar-foo-11.11-22.11

            $ rev ip.txt
            rab-oof-o.of-11.11-oof
            11.22-11.11-oof-rab

            $ rev ip.txt | cut -d- -f3- | rev
            foo-11.11-fo.o
            bar-foo


            Reverse each line, then use cut to select all fields except first two and then reverse the output again





            You can also use perl, but would print empty lines if any input line has less than 3 fields



            $ perl -F'-' -lane 'print join "-", @F[0..$#F-2]' ip.txt 
            foo-11.11-fo.o
            bar-foo


            Specify - as input delimiter and then print all but last two fields






            share|improve this answer




















            • rev that's what I missed. thanks a lot, working great.
              – Buvanesh Kumar
              Sep 25 '17 at 12:18














            up vote
            2
            down vote



            accepted










            $ cat ip.txt 
            foo-11.11-fo.o-foo-bar
            bar-foo-11.11-22.11

            $ rev ip.txt
            rab-oof-o.of-11.11-oof
            11.22-11.11-oof-rab

            $ rev ip.txt | cut -d- -f3- | rev
            foo-11.11-fo.o
            bar-foo


            Reverse each line, then use cut to select all fields except first two and then reverse the output again





            You can also use perl, but would print empty lines if any input line has less than 3 fields



            $ perl -F'-' -lane 'print join "-", @F[0..$#F-2]' ip.txt 
            foo-11.11-fo.o
            bar-foo


            Specify - as input delimiter and then print all but last two fields






            share|improve this answer




















            • rev that's what I missed. thanks a lot, working great.
              – Buvanesh Kumar
              Sep 25 '17 at 12:18












            up vote
            2
            down vote



            accepted







            up vote
            2
            down vote



            accepted






            $ cat ip.txt 
            foo-11.11-fo.o-foo-bar
            bar-foo-11.11-22.11

            $ rev ip.txt
            rab-oof-o.of-11.11-oof
            11.22-11.11-oof-rab

            $ rev ip.txt | cut -d- -f3- | rev
            foo-11.11-fo.o
            bar-foo


            Reverse each line, then use cut to select all fields except first two and then reverse the output again





            You can also use perl, but would print empty lines if any input line has less than 3 fields



            $ perl -F'-' -lane 'print join "-", @F[0..$#F-2]' ip.txt 
            foo-11.11-fo.o
            bar-foo


            Specify - as input delimiter and then print all but last two fields






            share|improve this answer












            $ cat ip.txt 
            foo-11.11-fo.o-foo-bar
            bar-foo-11.11-22.11

            $ rev ip.txt
            rab-oof-o.of-11.11-oof
            11.22-11.11-oof-rab

            $ rev ip.txt | cut -d- -f3- | rev
            foo-11.11-fo.o
            bar-foo


            Reverse each line, then use cut to select all fields except first two and then reverse the output again





            You can also use perl, but would print empty lines if any input line has less than 3 fields



            $ perl -F'-' -lane 'print join "-", @F[0..$#F-2]' ip.txt 
            foo-11.11-fo.o
            bar-foo


            Specify - as input delimiter and then print all but last two fields







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Sep 25 '17 at 12:00









            Sundeep

            6,9711826




            6,9711826











            • rev that's what I missed. thanks a lot, working great.
              – Buvanesh Kumar
              Sep 25 '17 at 12:18
















            • rev that's what I missed. thanks a lot, working great.
              – Buvanesh Kumar
              Sep 25 '17 at 12:18















            rev that's what I missed. thanks a lot, working great.
            – Buvanesh Kumar
            Sep 25 '17 at 12:18




            rev that's what I missed. thanks a lot, working great.
            – Buvanesh Kumar
            Sep 25 '17 at 12:18












            up vote
            4
            down vote













            Using sed:



            $ sed 's/-[^-]*-[^-]*$//' file
            foo-11.11-fo.o
            bar-foo


            This will remove -X-X at the end of every line in file, where X is any string that does not include a -.



            If the strings are in a shell variable:



            $ s='foo-11.11-fo.o-foo-bar'
            $ printf '%sn' "$s%-*-*"
            foo-11.11-fo.o

            $ s='bar-foo-11.11-22.11'
            $ printf '%sn' "$s%-*-*"
            bar-foo


            or in a bash array:



            $ s=( 'foo-11.11-fo.o-foo-bar' 'bar-foo-11.11-22.11' )
            $ printf '%sn' "$s[@]%-*-*"
            foo-11.11-fo.o
            bar-foo


            This removes anything matching the pattern -*-* at the end of the string in the variable s through a suffix pattern match/removal. In the case where s in an array, the removal is done on all elements of the array.






            share|improve this answer


























              up vote
              4
              down vote













              Using sed:



              $ sed 's/-[^-]*-[^-]*$//' file
              foo-11.11-fo.o
              bar-foo


              This will remove -X-X at the end of every line in file, where X is any string that does not include a -.



              If the strings are in a shell variable:



              $ s='foo-11.11-fo.o-foo-bar'
              $ printf '%sn' "$s%-*-*"
              foo-11.11-fo.o

              $ s='bar-foo-11.11-22.11'
              $ printf '%sn' "$s%-*-*"
              bar-foo


              or in a bash array:



              $ s=( 'foo-11.11-fo.o-foo-bar' 'bar-foo-11.11-22.11' )
              $ printf '%sn' "$s[@]%-*-*"
              foo-11.11-fo.o
              bar-foo


              This removes anything matching the pattern -*-* at the end of the string in the variable s through a suffix pattern match/removal. In the case where s in an array, the removal is done on all elements of the array.






              share|improve this answer
























                up vote
                4
                down vote










                up vote
                4
                down vote









                Using sed:



                $ sed 's/-[^-]*-[^-]*$//' file
                foo-11.11-fo.o
                bar-foo


                This will remove -X-X at the end of every line in file, where X is any string that does not include a -.



                If the strings are in a shell variable:



                $ s='foo-11.11-fo.o-foo-bar'
                $ printf '%sn' "$s%-*-*"
                foo-11.11-fo.o

                $ s='bar-foo-11.11-22.11'
                $ printf '%sn' "$s%-*-*"
                bar-foo


                or in a bash array:



                $ s=( 'foo-11.11-fo.o-foo-bar' 'bar-foo-11.11-22.11' )
                $ printf '%sn' "$s[@]%-*-*"
                foo-11.11-fo.o
                bar-foo


                This removes anything matching the pattern -*-* at the end of the string in the variable s through a suffix pattern match/removal. In the case where s in an array, the removal is done on all elements of the array.






                share|improve this answer














                Using sed:



                $ sed 's/-[^-]*-[^-]*$//' file
                foo-11.11-fo.o
                bar-foo


                This will remove -X-X at the end of every line in file, where X is any string that does not include a -.



                If the strings are in a shell variable:



                $ s='foo-11.11-fo.o-foo-bar'
                $ printf '%sn' "$s%-*-*"
                foo-11.11-fo.o

                $ s='bar-foo-11.11-22.11'
                $ printf '%sn' "$s%-*-*"
                bar-foo


                or in a bash array:



                $ s=( 'foo-11.11-fo.o-foo-bar' 'bar-foo-11.11-22.11' )
                $ printf '%sn' "$s[@]%-*-*"
                foo-11.11-fo.o
                bar-foo


                This removes anything matching the pattern -*-* at the end of the string in the variable s through a suffix pattern match/removal. In the case where s in an array, the removal is done on all elements of the array.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Sep 25 '17 at 12:30

























                answered Sep 25 '17 at 12:02









                Kusalananda

                106k14209327




                106k14209327



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f394305%2fremove-string-using-certain-values%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?