Remove one or more fields, delimited by a “-”, at end of lineHow to remove trailing whitespace at the end of the line in given files (more than one)?Print certain fields of each line until a marker is encountered, then print whole lines till the end of filesed remove end of line for specific linesAWK remove one line?Comparing delimited fieldsConcatenate multiple fields separately based on one (key) columnPrinting more than one fieldAwk: remove first few fields from CSVawk remove lines with digits at endFormat Date fields in Pipe Delimited File

Do I need to rip the leaves off mint?

What are the ethical implications of lying to get into a course?

Why doesn't the road lose its thickness to the tyre?

What specifically can swap do that RAM can't

Array elements of struct and struct members

Keeping a healthy immune system on a generation-ship

Can I be fired the same day that I hand in my notice?

Reimbursed more than my travel expenses for interview

Sci-fi novel from 1980s(?) about colony on Mars

When its not okay to cheap out on bike parts

How to teach children Santa is not real, while respecting other kids beliefs?

Buy Land Using Old 401K?

Python Code for List of Month Names starting with current month

How important is quick release for a tripod?

Importing a Wikipedia Table as a Dataset?

Isn't Social Security set up as a Pension Fund as opposed to a Direct Transfers Scheme?

Can a human colony survive on a 'hot' world?

Black screen for 1-2 seconds while alt-tabbing a fullscreen game or using a Windows key

Obtaining the terms of a summation alongside the result

When was Newton "not good enough" for spaceflight; first use and first absolute requirement for relativistic corrections?

Why should you have travel insurance?

Do any countries have a pensions system funded entirely by past contributions, rather than current taxes?

Are unitarily equivalent permutation matrices permutation similar?

Why don't all States switch to all postal voting?



Remove one or more fields, delimited by a “-”, at end of line


How to remove trailing whitespace at the end of the line in given files (more than one)?Print certain fields of each line until a marker is encountered, then print whole lines till the end of filesed remove end of line for specific linesAWK remove one line?Comparing delimited fieldsConcatenate multiple fields separately based on one (key) columnPrinting more than one fieldAwk: remove first few fields from CSVawk remove lines with digits at endFormat Date fields in Pipe Delimited File






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









8


















I am going to parse data googleapis.txt



bucket,abc-def-ghi-45gjd4-wwxis
bucket,dde-wwq-ooi-66ciow-po22q
instance,jkl-mno-1-zzz-68dkakw-oo9w8
disk,pqr-stu-10-kuy-l2oxapw-rp4lt


I expect the result like these below



bucket,abc-def-ghi
bucket,dde-wwq-ooi
instance,jkl-mno-1-zzz
disk,pqr-stu-10-kuy


I am thinking that i have to change - to be a space and then run this command



cat googleapis.txt | awk '$NF="";sub(/[ t]+$/,"")1' | awk '$NF="";sub(/[ t]+$/,"")1'


I got that from this https://stackoverflow.com/a/27794421/8162936
After parsed, i will change the space to be a hypen - back.



Does anyone know the best practice or one-liner shell command to parse it ?
Thanks all










share|improve this question

































    8


















    I am going to parse data googleapis.txt



    bucket,abc-def-ghi-45gjd4-wwxis
    bucket,dde-wwq-ooi-66ciow-po22q
    instance,jkl-mno-1-zzz-68dkakw-oo9w8
    disk,pqr-stu-10-kuy-l2oxapw-rp4lt


    I expect the result like these below



    bucket,abc-def-ghi
    bucket,dde-wwq-ooi
    instance,jkl-mno-1-zzz
    disk,pqr-stu-10-kuy


    I am thinking that i have to change - to be a space and then run this command



    cat googleapis.txt | awk '$NF="";sub(/[ t]+$/,"")1' | awk '$NF="";sub(/[ t]+$/,"")1'


    I got that from this https://stackoverflow.com/a/27794421/8162936
    After parsed, i will change the space to be a hypen - back.



    Does anyone know the best practice or one-liner shell command to parse it ?
    Thanks all










    share|improve this question





























      8













      8









      8


      1






      I am going to parse data googleapis.txt



      bucket,abc-def-ghi-45gjd4-wwxis
      bucket,dde-wwq-ooi-66ciow-po22q
      instance,jkl-mno-1-zzz-68dkakw-oo9w8
      disk,pqr-stu-10-kuy-l2oxapw-rp4lt


      I expect the result like these below



      bucket,abc-def-ghi
      bucket,dde-wwq-ooi
      instance,jkl-mno-1-zzz
      disk,pqr-stu-10-kuy


      I am thinking that i have to change - to be a space and then run this command



      cat googleapis.txt | awk '$NF="";sub(/[ t]+$/,"")1' | awk '$NF="";sub(/[ t]+$/,"")1'


      I got that from this https://stackoverflow.com/a/27794421/8162936
      After parsed, i will change the space to be a hypen - back.



      Does anyone know the best practice or one-liner shell command to parse it ?
      Thanks all










      share|improve this question
















      I am going to parse data googleapis.txt



      bucket,abc-def-ghi-45gjd4-wwxis
      bucket,dde-wwq-ooi-66ciow-po22q
      instance,jkl-mno-1-zzz-68dkakw-oo9w8
      disk,pqr-stu-10-kuy-l2oxapw-rp4lt


      I expect the result like these below



      bucket,abc-def-ghi
      bucket,dde-wwq-ooi
      instance,jkl-mno-1-zzz
      disk,pqr-stu-10-kuy


      I am thinking that i have to change - to be a space and then run this command



      cat googleapis.txt | awk '$NF="";sub(/[ t]+$/,"")1' | awk '$NF="";sub(/[ t]+$/,"")1'


      I got that from this https://stackoverflow.com/a/27794421/8162936
      After parsed, i will change the space to be a hypen - back.



      Does anyone know the best practice or one-liner shell command to parse it ?
      Thanks all







      text-processing awk






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 30 at 0:16









      cas

      45.8k5 gold badges71 silver badges124 bronze badges




      45.8k5 gold badges71 silver badges124 bronze badges










      asked Sep 29 at 10:03









      Nicky PuffNicky Puff

      1176 bronze badges




      1176 bronze badges























          5 Answers
          5






          active

          oldest

          votes


















          10



















          with sed you can do:



          sed -E 's/(-[^-]*)2$//' infile


          match a pattern like -anything twice (...)2 from end $ of every line and remove it.






          share|improve this answer
































            7



















            $ sed 's/-[[:alnum:]]*-[[:alnum:]]*$//' file
            bucket,abc-def-ghi
            bucket,dde-wwq-ooi
            instance,jkl-mno-1-zzz
            disk,pqr-stu-10-kuy


            This uses sed to match the last two dash-delimited substrings on each line and remove them. [[:alnum:]] will match any alphanumeric character.



            You may shorten it down to



            sed 's/(-[[:alnum:]]*)2$//' file


            i.e., match and delete two sets of -[[:alnum:]]* ath the end of each line.



            With GNU awk, you could also do



            $ awk -F '-' 'BEGIN OFS=FS NF -= 2; print ' file
            bucket,abc-def-ghi
            bucket,dde-wwq-ooi
            instance,jkl-mno-1-zzz
            disk,pqr-stu-10-kuy


            but changing NF like this is not portable, and should be avoided (there's no guarantee that it changes the current record). It would not work with BSD awk, for example.



            With standard awk, without resorting to using sub() (which would be to just mimic sed), you would have to recreate the current record from the fields that you'd want to use (in our case, all but the last two dash-delimited fields):



            $ awk -F '-' 'BEGIN OFS=FS nf = split($0,a) - 2; $0=""; for (i=1; i<=nf; ++i) $i = a[i]; print ' file
            bucket,abc-def-ghi
            bucket,dde-wwq-ooi
            instance,jkl-mno-1-zzz
            disk,pqr-stu-10-kuy





            share|improve this answer


































              4



















              With rev and cut:



              rev file | cut -d'-' -f3- | rev


              Reverse the lines, cut field 3 to the end of the line and reverse the text back again.




              With grep (and PCRE):



              grep -Po '.*(?=(-[^-]*)2$)' file



              • -P use perl-compatible regular expressions with a positive lookahead (?...) containing two matches of - followed by any non-- characters


              • -o print only matched parts





              share|improve this answer
































                4



















                $ perl -F- -lane 'print join "-", @F[0..($#F-2)]' googleapis.txt
                bucket,abc-def-ghi
                bucket,dde-wwq-ooi
                instance,jkl-mno-1-zzz
                disk,pqr-stu-10-kuy


                This autosplits each input line into array @F, using delimiter -.



                Then it prints an array slice of all but the last two fields, re-joined with - characters.






                share|improve this answer
































                  1



















                  you can do it various ways as shown here:



                  $ perl -F- -pale '$"="-";$#F-=2;$_="@F"' file


                  Split the lines on a dash, set the array element joiner to dash, clip the last two elements, and set the current line to array joined with dashes.



                  $ awk -F- '
                  t = $1
                  for ( i=2; i<NF-1; i++ ) t = t FS $i
                  $0 = t
                  1' file


                  This is with plain string processing:



                  $ perl -lne 'print substr($_, 0, rindex($_,"-",-1+rindex($_,"-")))' file


                  .



                  $ sed -ne '
                  y/-/n/
                  :a;h;s/n/-/;/n.*n/ba
                  g;P
                  ' file


                  Results:



                  bucket,abc-def-ghi
                  bucket,dde-wwq-ooi
                  instance,jkl-mno-1-zzz
                  disk,pqr-stu-10-kuy





                  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',
                    autoActivateHeartbeat: false,
                    convertImagesToLinks: false,
                    noModals: true,
                    showLowRepImageUploadWarning: true,
                    reputationToPostImages: null,
                    bindNavPrevention: true,
                    postfix: "",
                    imageUploader:
                    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                    allowUrls: true
                    ,
                    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%2f544299%2fremove-one-or-more-fields-delimited-by-a-at-end-of-line%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown


























                    5 Answers
                    5






                    active

                    oldest

                    votes








                    5 Answers
                    5






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes









                    10



















                    with sed you can do:



                    sed -E 's/(-[^-]*)2$//' infile


                    match a pattern like -anything twice (...)2 from end $ of every line and remove it.






                    share|improve this answer





























                      10



















                      with sed you can do:



                      sed -E 's/(-[^-]*)2$//' infile


                      match a pattern like -anything twice (...)2 from end $ of every line and remove it.






                      share|improve this answer



























                        10















                        10











                        10









                        with sed you can do:



                        sed -E 's/(-[^-]*)2$//' infile


                        match a pattern like -anything twice (...)2 from end $ of every line and remove it.






                        share|improve this answer














                        with sed you can do:



                        sed -E 's/(-[^-]*)2$//' infile


                        match a pattern like -anything twice (...)2 from end $ of every line and remove it.







                        share|improve this answer













                        share|improve this answer




                        share|improve this answer










                        answered Sep 29 at 10:16









                        αғsнιηαғsнιη

                        20.7k11 gold badges36 silver badges74 bronze badges




                        20.7k11 gold badges36 silver badges74 bronze badges


























                            7



















                            $ sed 's/-[[:alnum:]]*-[[:alnum:]]*$//' file
                            bucket,abc-def-ghi
                            bucket,dde-wwq-ooi
                            instance,jkl-mno-1-zzz
                            disk,pqr-stu-10-kuy


                            This uses sed to match the last two dash-delimited substrings on each line and remove them. [[:alnum:]] will match any alphanumeric character.



                            You may shorten it down to



                            sed 's/(-[[:alnum:]]*)2$//' file


                            i.e., match and delete two sets of -[[:alnum:]]* ath the end of each line.



                            With GNU awk, you could also do



                            $ awk -F '-' 'BEGIN OFS=FS NF -= 2; print ' file
                            bucket,abc-def-ghi
                            bucket,dde-wwq-ooi
                            instance,jkl-mno-1-zzz
                            disk,pqr-stu-10-kuy


                            but changing NF like this is not portable, and should be avoided (there's no guarantee that it changes the current record). It would not work with BSD awk, for example.



                            With standard awk, without resorting to using sub() (which would be to just mimic sed), you would have to recreate the current record from the fields that you'd want to use (in our case, all but the last two dash-delimited fields):



                            $ awk -F '-' 'BEGIN OFS=FS nf = split($0,a) - 2; $0=""; for (i=1; i<=nf; ++i) $i = a[i]; print ' file
                            bucket,abc-def-ghi
                            bucket,dde-wwq-ooi
                            instance,jkl-mno-1-zzz
                            disk,pqr-stu-10-kuy





                            share|improve this answer































                              7



















                              $ sed 's/-[[:alnum:]]*-[[:alnum:]]*$//' file
                              bucket,abc-def-ghi
                              bucket,dde-wwq-ooi
                              instance,jkl-mno-1-zzz
                              disk,pqr-stu-10-kuy


                              This uses sed to match the last two dash-delimited substrings on each line and remove them. [[:alnum:]] will match any alphanumeric character.



                              You may shorten it down to



                              sed 's/(-[[:alnum:]]*)2$//' file


                              i.e., match and delete two sets of -[[:alnum:]]* ath the end of each line.



                              With GNU awk, you could also do



                              $ awk -F '-' 'BEGIN OFS=FS NF -= 2; print ' file
                              bucket,abc-def-ghi
                              bucket,dde-wwq-ooi
                              instance,jkl-mno-1-zzz
                              disk,pqr-stu-10-kuy


                              but changing NF like this is not portable, and should be avoided (there's no guarantee that it changes the current record). It would not work with BSD awk, for example.



                              With standard awk, without resorting to using sub() (which would be to just mimic sed), you would have to recreate the current record from the fields that you'd want to use (in our case, all but the last two dash-delimited fields):



                              $ awk -F '-' 'BEGIN OFS=FS nf = split($0,a) - 2; $0=""; for (i=1; i<=nf; ++i) $i = a[i]; print ' file
                              bucket,abc-def-ghi
                              bucket,dde-wwq-ooi
                              instance,jkl-mno-1-zzz
                              disk,pqr-stu-10-kuy





                              share|improve this answer





























                                7















                                7











                                7









                                $ sed 's/-[[:alnum:]]*-[[:alnum:]]*$//' file
                                bucket,abc-def-ghi
                                bucket,dde-wwq-ooi
                                instance,jkl-mno-1-zzz
                                disk,pqr-stu-10-kuy


                                This uses sed to match the last two dash-delimited substrings on each line and remove them. [[:alnum:]] will match any alphanumeric character.



                                You may shorten it down to



                                sed 's/(-[[:alnum:]]*)2$//' file


                                i.e., match and delete two sets of -[[:alnum:]]* ath the end of each line.



                                With GNU awk, you could also do



                                $ awk -F '-' 'BEGIN OFS=FS NF -= 2; print ' file
                                bucket,abc-def-ghi
                                bucket,dde-wwq-ooi
                                instance,jkl-mno-1-zzz
                                disk,pqr-stu-10-kuy


                                but changing NF like this is not portable, and should be avoided (there's no guarantee that it changes the current record). It would not work with BSD awk, for example.



                                With standard awk, without resorting to using sub() (which would be to just mimic sed), you would have to recreate the current record from the fields that you'd want to use (in our case, all but the last two dash-delimited fields):



                                $ awk -F '-' 'BEGIN OFS=FS nf = split($0,a) - 2; $0=""; for (i=1; i<=nf; ++i) $i = a[i]; print ' file
                                bucket,abc-def-ghi
                                bucket,dde-wwq-ooi
                                instance,jkl-mno-1-zzz
                                disk,pqr-stu-10-kuy





                                share|improve this answer
















                                $ sed 's/-[[:alnum:]]*-[[:alnum:]]*$//' file
                                bucket,abc-def-ghi
                                bucket,dde-wwq-ooi
                                instance,jkl-mno-1-zzz
                                disk,pqr-stu-10-kuy


                                This uses sed to match the last two dash-delimited substrings on each line and remove them. [[:alnum:]] will match any alphanumeric character.



                                You may shorten it down to



                                sed 's/(-[[:alnum:]]*)2$//' file


                                i.e., match and delete two sets of -[[:alnum:]]* ath the end of each line.



                                With GNU awk, you could also do



                                $ awk -F '-' 'BEGIN OFS=FS NF -= 2; print ' file
                                bucket,abc-def-ghi
                                bucket,dde-wwq-ooi
                                instance,jkl-mno-1-zzz
                                disk,pqr-stu-10-kuy


                                but changing NF like this is not portable, and should be avoided (there's no guarantee that it changes the current record). It would not work with BSD awk, for example.



                                With standard awk, without resorting to using sub() (which would be to just mimic sed), you would have to recreate the current record from the fields that you'd want to use (in our case, all but the last two dash-delimited fields):



                                $ awk -F '-' 'BEGIN OFS=FS nf = split($0,a) - 2; $0=""; for (i=1; i<=nf; ++i) $i = a[i]; print ' file
                                bucket,abc-def-ghi
                                bucket,dde-wwq-ooi
                                instance,jkl-mno-1-zzz
                                disk,pqr-stu-10-kuy






                                share|improve this answer















                                share|improve this answer




                                share|improve this answer








                                edited Sep 29 at 11:11

























                                answered Sep 29 at 10:16









                                KusalanandaKusalananda

                                176k21 gold badges334 silver badges542 bronze badges




                                176k21 gold badges334 silver badges542 bronze badges
























                                    4



















                                    With rev and cut:



                                    rev file | cut -d'-' -f3- | rev


                                    Reverse the lines, cut field 3 to the end of the line and reverse the text back again.




                                    With grep (and PCRE):



                                    grep -Po '.*(?=(-[^-]*)2$)' file



                                    • -P use perl-compatible regular expressions with a positive lookahead (?...) containing two matches of - followed by any non-- characters


                                    • -o print only matched parts





                                    share|improve this answer





























                                      4



















                                      With rev and cut:



                                      rev file | cut -d'-' -f3- | rev


                                      Reverse the lines, cut field 3 to the end of the line and reverse the text back again.




                                      With grep (and PCRE):



                                      grep -Po '.*(?=(-[^-]*)2$)' file



                                      • -P use perl-compatible regular expressions with a positive lookahead (?...) containing two matches of - followed by any non-- characters


                                      • -o print only matched parts





                                      share|improve this answer



























                                        4















                                        4











                                        4









                                        With rev and cut:



                                        rev file | cut -d'-' -f3- | rev


                                        Reverse the lines, cut field 3 to the end of the line and reverse the text back again.




                                        With grep (and PCRE):



                                        grep -Po '.*(?=(-[^-]*)2$)' file



                                        • -P use perl-compatible regular expressions with a positive lookahead (?...) containing two matches of - followed by any non-- characters


                                        • -o print only matched parts





                                        share|improve this answer














                                        With rev and cut:



                                        rev file | cut -d'-' -f3- | rev


                                        Reverse the lines, cut field 3 to the end of the line and reverse the text back again.




                                        With grep (and PCRE):



                                        grep -Po '.*(?=(-[^-]*)2$)' file



                                        • -P use perl-compatible regular expressions with a positive lookahead (?...) containing two matches of - followed by any non-- characters


                                        • -o print only matched parts






                                        share|improve this answer













                                        share|improve this answer




                                        share|improve this answer










                                        answered Sep 29 at 10:24









                                        FreddyFreddy

                                        11k1 gold badge8 silver badges39 bronze badges




                                        11k1 gold badge8 silver badges39 bronze badges
























                                            4



















                                            $ perl -F- -lane 'print join "-", @F[0..($#F-2)]' googleapis.txt
                                            bucket,abc-def-ghi
                                            bucket,dde-wwq-ooi
                                            instance,jkl-mno-1-zzz
                                            disk,pqr-stu-10-kuy


                                            This autosplits each input line into array @F, using delimiter -.



                                            Then it prints an array slice of all but the last two fields, re-joined with - characters.






                                            share|improve this answer





























                                              4



















                                              $ perl -F- -lane 'print join "-", @F[0..($#F-2)]' googleapis.txt
                                              bucket,abc-def-ghi
                                              bucket,dde-wwq-ooi
                                              instance,jkl-mno-1-zzz
                                              disk,pqr-stu-10-kuy


                                              This autosplits each input line into array @F, using delimiter -.



                                              Then it prints an array slice of all but the last two fields, re-joined with - characters.






                                              share|improve this answer



























                                                4















                                                4











                                                4









                                                $ perl -F- -lane 'print join "-", @F[0..($#F-2)]' googleapis.txt
                                                bucket,abc-def-ghi
                                                bucket,dde-wwq-ooi
                                                instance,jkl-mno-1-zzz
                                                disk,pqr-stu-10-kuy


                                                This autosplits each input line into array @F, using delimiter -.



                                                Then it prints an array slice of all but the last two fields, re-joined with - characters.






                                                share|improve this answer














                                                $ perl -F- -lane 'print join "-", @F[0..($#F-2)]' googleapis.txt
                                                bucket,abc-def-ghi
                                                bucket,dde-wwq-ooi
                                                instance,jkl-mno-1-zzz
                                                disk,pqr-stu-10-kuy


                                                This autosplits each input line into array @F, using delimiter -.



                                                Then it prints an array slice of all but the last two fields, re-joined with - characters.







                                                share|improve this answer













                                                share|improve this answer




                                                share|improve this answer










                                                answered Sep 29 at 11:26









                                                cascas

                                                45.8k5 gold badges71 silver badges124 bronze badges




                                                45.8k5 gold badges71 silver badges124 bronze badges
























                                                    1



















                                                    you can do it various ways as shown here:



                                                    $ perl -F- -pale '$"="-";$#F-=2;$_="@F"' file


                                                    Split the lines on a dash, set the array element joiner to dash, clip the last two elements, and set the current line to array joined with dashes.



                                                    $ awk -F- '
                                                    t = $1
                                                    for ( i=2; i<NF-1; i++ ) t = t FS $i
                                                    $0 = t
                                                    1' file


                                                    This is with plain string processing:



                                                    $ perl -lne 'print substr($_, 0, rindex($_,"-",-1+rindex($_,"-")))' file


                                                    .



                                                    $ sed -ne '
                                                    y/-/n/
                                                    :a;h;s/n/-/;/n.*n/ba
                                                    g;P
                                                    ' file


                                                    Results:



                                                    bucket,abc-def-ghi
                                                    bucket,dde-wwq-ooi
                                                    instance,jkl-mno-1-zzz
                                                    disk,pqr-stu-10-kuy





                                                    share|improve this answer





























                                                      1



















                                                      you can do it various ways as shown here:



                                                      $ perl -F- -pale '$"="-";$#F-=2;$_="@F"' file


                                                      Split the lines on a dash, set the array element joiner to dash, clip the last two elements, and set the current line to array joined with dashes.



                                                      $ awk -F- '
                                                      t = $1
                                                      for ( i=2; i<NF-1; i++ ) t = t FS $i
                                                      $0 = t
                                                      1' file


                                                      This is with plain string processing:



                                                      $ perl -lne 'print substr($_, 0, rindex($_,"-",-1+rindex($_,"-")))' file


                                                      .



                                                      $ sed -ne '
                                                      y/-/n/
                                                      :a;h;s/n/-/;/n.*n/ba
                                                      g;P
                                                      ' file


                                                      Results:



                                                      bucket,abc-def-ghi
                                                      bucket,dde-wwq-ooi
                                                      instance,jkl-mno-1-zzz
                                                      disk,pqr-stu-10-kuy





                                                      share|improve this answer



























                                                        1















                                                        1











                                                        1









                                                        you can do it various ways as shown here:



                                                        $ perl -F- -pale '$"="-";$#F-=2;$_="@F"' file


                                                        Split the lines on a dash, set the array element joiner to dash, clip the last two elements, and set the current line to array joined with dashes.



                                                        $ awk -F- '
                                                        t = $1
                                                        for ( i=2; i<NF-1; i++ ) t = t FS $i
                                                        $0 = t
                                                        1' file


                                                        This is with plain string processing:



                                                        $ perl -lne 'print substr($_, 0, rindex($_,"-",-1+rindex($_,"-")))' file


                                                        .



                                                        $ sed -ne '
                                                        y/-/n/
                                                        :a;h;s/n/-/;/n.*n/ba
                                                        g;P
                                                        ' file


                                                        Results:



                                                        bucket,abc-def-ghi
                                                        bucket,dde-wwq-ooi
                                                        instance,jkl-mno-1-zzz
                                                        disk,pqr-stu-10-kuy





                                                        share|improve this answer














                                                        you can do it various ways as shown here:



                                                        $ perl -F- -pale '$"="-";$#F-=2;$_="@F"' file


                                                        Split the lines on a dash, set the array element joiner to dash, clip the last two elements, and set the current line to array joined with dashes.



                                                        $ awk -F- '
                                                        t = $1
                                                        for ( i=2; i<NF-1; i++ ) t = t FS $i
                                                        $0 = t
                                                        1' file


                                                        This is with plain string processing:



                                                        $ perl -lne 'print substr($_, 0, rindex($_,"-",-1+rindex($_,"-")))' file


                                                        .



                                                        $ sed -ne '
                                                        y/-/n/
                                                        :a;h;s/n/-/;/n.*n/ba
                                                        g;P
                                                        ' file


                                                        Results:



                                                        bucket,abc-def-ghi
                                                        bucket,dde-wwq-ooi
                                                        instance,jkl-mno-1-zzz
                                                        disk,pqr-stu-10-kuy






                                                        share|improve this answer













                                                        share|improve this answer




                                                        share|improve this answer










                                                        answered Sep 30 at 14:48









                                                        Rakesh SharmaRakesh Sharma

                                                        3833 bronze badges




                                                        3833 bronze badges































                                                            draft saved

                                                            draft discarded















































                                                            Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                                            • Please be sure to answer the question. Provide details and share your research!

                                                            But avoid


                                                            • Asking for help, clarification, or responding to other answers.

                                                            • Making statements based on opinion; back them up with references or personal experience.

                                                            To learn more, see our tips on writing great answers.




                                                            draft saved


                                                            draft discarded














                                                            StackExchange.ready(
                                                            function ()
                                                            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f544299%2fremove-one-or-more-fields-delimited-by-a-at-end-of-line%23new-answer', 'question_page');

                                                            );

                                                            Post as a guest















                                                            Required, but never shown





















































                                                            Required, but never shown














                                                            Required, but never shown












                                                            Required, but never shown







                                                            Required, but never shown

































                                                            Required, but never shown














                                                            Required, but never shown












                                                            Required, but never shown







                                                            Required, but never shown









                                                            Popular posts from this blog

                                                            Tamil (spriik) Luke uk diar | Nawigatjuun

                                                            Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

                                                            Where does the image of a data connector as a sharp metal spike originate from?Where does the concept of infected people turning into zombies only after death originate from?Where does the motif of a reanimated human head originate?Where did the notion that Dragons could speak originate?Where does the archetypal image of the 'Grey' alien come from?Where did the suffix '-Man' originate?Where does the notion of being injured or killed by an illusion originate?Where did the term “sophont” originate?Where does the trope of magic spells being driven by advanced technology originate from?Where did the term “the living impaired” originate?