Get the last element of a row, separated with ;











up vote
0
down vote

favorite












I have a file (test.txt), where the separator is ;. How can I write out the last element from each row? (the length of each element is different)










share|improve this question




























    up vote
    0
    down vote

    favorite












    I have a file (test.txt), where the separator is ;. How can I write out the last element from each row? (the length of each element is different)










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a file (test.txt), where the separator is ;. How can I write out the last element from each row? (the length of each element is different)










      share|improve this question















      I have a file (test.txt), where the separator is ;. How can I write out the last element from each row? (the length of each element is different)







      text-processing






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 5 at 11:36









      Debian_yadav

      1,3653922




      1,3653922










      asked Dec 5 at 9:25









      Krsztr

      83




      83






















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          4
          down vote













          sed 's/.*;//' < test.txt


          Deletes everything up to the last ; on each line.



          Or with awk:



          awk -F';' '{print $NF}' < test.txt


          If you know in advance the number of fields, you can use cut. For instance, if there are 4 fields:



          cut -d';' -f4 < test.txt


          Beware though that if there are fewer than 4 fields on a line, then it will return an empty line except if there's only one field (no ; in the line), in which case it will output the full line (that first field). A known misfeature in cut.






          share|improve this answer






























            up vote
            0
            down vote













            #!/bin/bash
            while read line;
            do
            echo $(echo $line | awk -F ';' '{print $NF}')
            done < test.txt


            This is a simple script this will take each line of test.txt as input and will the print the last element separated by ;.






            share|improve this answer



















            • 1




              Starting a new awk for every line seems a bit wasteful.
              – Ljm Dullaart
              Dec 5 at 10:43










            • You may want to read Why is using a shell loop to process text considered bad practice?, Security implications of forgetting to quote a variable in bash/POSIX shells, Understanding "IFS= read -r line", Why is printf better than echo?
              – Stéphane Chazelas
              Dec 5 at 10:44










            • Thanks actually I am beginner to shell script.
              – Debian_yadav
              Dec 5 at 12:23


















            up vote
            0
            down vote













            Or in bash:



            #!/bin/bash

            while read line ; do
            echo ${a##*;}
            done





            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: 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/3.0/"u003ecc by-sa 3.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%2f486106%2fget-the-last-element-of-a-row-separated-with%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              4
              down vote













              sed 's/.*;//' < test.txt


              Deletes everything up to the last ; on each line.



              Or with awk:



              awk -F';' '{print $NF}' < test.txt


              If you know in advance the number of fields, you can use cut. For instance, if there are 4 fields:



              cut -d';' -f4 < test.txt


              Beware though that if there are fewer than 4 fields on a line, then it will return an empty line except if there's only one field (no ; in the line), in which case it will output the full line (that first field). A known misfeature in cut.






              share|improve this answer



























                up vote
                4
                down vote













                sed 's/.*;//' < test.txt


                Deletes everything up to the last ; on each line.



                Or with awk:



                awk -F';' '{print $NF}' < test.txt


                If you know in advance the number of fields, you can use cut. For instance, if there are 4 fields:



                cut -d';' -f4 < test.txt


                Beware though that if there are fewer than 4 fields on a line, then it will return an empty line except if there's only one field (no ; in the line), in which case it will output the full line (that first field). A known misfeature in cut.






                share|improve this answer

























                  up vote
                  4
                  down vote










                  up vote
                  4
                  down vote









                  sed 's/.*;//' < test.txt


                  Deletes everything up to the last ; on each line.



                  Or with awk:



                  awk -F';' '{print $NF}' < test.txt


                  If you know in advance the number of fields, you can use cut. For instance, if there are 4 fields:



                  cut -d';' -f4 < test.txt


                  Beware though that if there are fewer than 4 fields on a line, then it will return an empty line except if there's only one field (no ; in the line), in which case it will output the full line (that first field). A known misfeature in cut.






                  share|improve this answer














                  sed 's/.*;//' < test.txt


                  Deletes everything up to the last ; on each line.



                  Or with awk:



                  awk -F';' '{print $NF}' < test.txt


                  If you know in advance the number of fields, you can use cut. For instance, if there are 4 fields:



                  cut -d';' -f4 < test.txt


                  Beware though that if there are fewer than 4 fields on a line, then it will return an empty line except if there's only one field (no ; in the line), in which case it will output the full line (that first field). A known misfeature in cut.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Dec 5 at 10:43

























                  answered Dec 5 at 10:35









                  Stéphane Chazelas

                  297k54562909




                  297k54562909
























                      up vote
                      0
                      down vote













                      #!/bin/bash
                      while read line;
                      do
                      echo $(echo $line | awk -F ';' '{print $NF}')
                      done < test.txt


                      This is a simple script this will take each line of test.txt as input and will the print the last element separated by ;.






                      share|improve this answer



















                      • 1




                        Starting a new awk for every line seems a bit wasteful.
                        – Ljm Dullaart
                        Dec 5 at 10:43










                      • You may want to read Why is using a shell loop to process text considered bad practice?, Security implications of forgetting to quote a variable in bash/POSIX shells, Understanding "IFS= read -r line", Why is printf better than echo?
                        – Stéphane Chazelas
                        Dec 5 at 10:44










                      • Thanks actually I am beginner to shell script.
                        – Debian_yadav
                        Dec 5 at 12:23















                      up vote
                      0
                      down vote













                      #!/bin/bash
                      while read line;
                      do
                      echo $(echo $line | awk -F ';' '{print $NF}')
                      done < test.txt


                      This is a simple script this will take each line of test.txt as input and will the print the last element separated by ;.






                      share|improve this answer



















                      • 1




                        Starting a new awk for every line seems a bit wasteful.
                        – Ljm Dullaart
                        Dec 5 at 10:43










                      • You may want to read Why is using a shell loop to process text considered bad practice?, Security implications of forgetting to quote a variable in bash/POSIX shells, Understanding "IFS= read -r line", Why is printf better than echo?
                        – Stéphane Chazelas
                        Dec 5 at 10:44










                      • Thanks actually I am beginner to shell script.
                        – Debian_yadav
                        Dec 5 at 12:23













                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      #!/bin/bash
                      while read line;
                      do
                      echo $(echo $line | awk -F ';' '{print $NF}')
                      done < test.txt


                      This is a simple script this will take each line of test.txt as input and will the print the last element separated by ;.






                      share|improve this answer














                      #!/bin/bash
                      while read line;
                      do
                      echo $(echo $line | awk -F ';' '{print $NF}')
                      done < test.txt


                      This is a simple script this will take each line of test.txt as input and will the print the last element separated by ;.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Dec 5 at 10:06

























                      answered Dec 5 at 9:47









                      Debian_yadav

                      1,3653922




                      1,3653922








                      • 1




                        Starting a new awk for every line seems a bit wasteful.
                        – Ljm Dullaart
                        Dec 5 at 10:43










                      • You may want to read Why is using a shell loop to process text considered bad practice?, Security implications of forgetting to quote a variable in bash/POSIX shells, Understanding "IFS= read -r line", Why is printf better than echo?
                        – Stéphane Chazelas
                        Dec 5 at 10:44










                      • Thanks actually I am beginner to shell script.
                        – Debian_yadav
                        Dec 5 at 12:23














                      • 1




                        Starting a new awk for every line seems a bit wasteful.
                        – Ljm Dullaart
                        Dec 5 at 10:43










                      • You may want to read Why is using a shell loop to process text considered bad practice?, Security implications of forgetting to quote a variable in bash/POSIX shells, Understanding "IFS= read -r line", Why is printf better than echo?
                        – Stéphane Chazelas
                        Dec 5 at 10:44










                      • Thanks actually I am beginner to shell script.
                        – Debian_yadav
                        Dec 5 at 12:23








                      1




                      1




                      Starting a new awk for every line seems a bit wasteful.
                      – Ljm Dullaart
                      Dec 5 at 10:43




                      Starting a new awk for every line seems a bit wasteful.
                      – Ljm Dullaart
                      Dec 5 at 10:43












                      You may want to read Why is using a shell loop to process text considered bad practice?, Security implications of forgetting to quote a variable in bash/POSIX shells, Understanding "IFS= read -r line", Why is printf better than echo?
                      – Stéphane Chazelas
                      Dec 5 at 10:44




                      You may want to read Why is using a shell loop to process text considered bad practice?, Security implications of forgetting to quote a variable in bash/POSIX shells, Understanding "IFS= read -r line", Why is printf better than echo?
                      – Stéphane Chazelas
                      Dec 5 at 10:44












                      Thanks actually I am beginner to shell script.
                      – Debian_yadav
                      Dec 5 at 12:23




                      Thanks actually I am beginner to shell script.
                      – Debian_yadav
                      Dec 5 at 12:23










                      up vote
                      0
                      down vote













                      Or in bash:



                      #!/bin/bash

                      while read line ; do
                      echo ${a##*;}
                      done





                      share|improve this answer

























                        up vote
                        0
                        down vote













                        Or in bash:



                        #!/bin/bash

                        while read line ; do
                        echo ${a##*;}
                        done





                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          Or in bash:



                          #!/bin/bash

                          while read line ; do
                          echo ${a##*;}
                          done





                          share|improve this answer












                          Or in bash:



                          #!/bin/bash

                          while read line ; do
                          echo ${a##*;}
                          done






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Dec 5 at 10:41









                          Ljm Dullaart

                          57517




                          57517






























                              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.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • 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%2f486106%2fget-the-last-element-of-a-row-separated-with%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

                              Morgemoulin

                              Scott Moir

                              Souastre