how to find the last word in file & ignore empty spaces

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











up vote
4
down vote

favorite












how to find the last word in file even after word there are empty lines



the case



I tried this to find the last word



tail -1 /tmp/requests.txt


but no output



I try the following approach



 awk 'END print $NF' /tmp/requests.txt


but no output



I capture the word only by tail -6 ( because the word was 6 empty lines before the end of the file



 tail -6 /tmp/requests.txt
"IN_PROGRESS"


so what is the way to capture the last word in case we have empty space or not



expected results



 echo $Last_word
IN_PROGRESS






share|improve this question


























    up vote
    4
    down vote

    favorite












    how to find the last word in file even after word there are empty lines



    the case



    I tried this to find the last word



    tail -1 /tmp/requests.txt


    but no output



    I try the following approach



     awk 'END print $NF' /tmp/requests.txt


    but no output



    I capture the word only by tail -6 ( because the word was 6 empty lines before the end of the file



     tail -6 /tmp/requests.txt
    "IN_PROGRESS"


    so what is the way to capture the last word in case we have empty space or not



    expected results



     echo $Last_word
    IN_PROGRESS






    share|improve this question
























      up vote
      4
      down vote

      favorite









      up vote
      4
      down vote

      favorite











      how to find the last word in file even after word there are empty lines



      the case



      I tried this to find the last word



      tail -1 /tmp/requests.txt


      but no output



      I try the following approach



       awk 'END print $NF' /tmp/requests.txt


      but no output



      I capture the word only by tail -6 ( because the word was 6 empty lines before the end of the file



       tail -6 /tmp/requests.txt
      "IN_PROGRESS"


      so what is the way to capture the last word in case we have empty space or not



      expected results



       echo $Last_word
      IN_PROGRESS






      share|improve this question














      how to find the last word in file even after word there are empty lines



      the case



      I tried this to find the last word



      tail -1 /tmp/requests.txt


      but no output



      I try the following approach



       awk 'END print $NF' /tmp/requests.txt


      but no output



      I capture the word only by tail -6 ( because the word was 6 empty lines before the end of the file



       tail -6 /tmp/requests.txt
      "IN_PROGRESS"


      so what is the way to capture the last word in case we have empty space or not



      expected results



       echo $Last_word
      IN_PROGRESS








      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 3 at 15:04









      Jeff Schaller

      31.8k848109




      31.8k848109










      asked Jan 3 at 14:47









      yael

      2,0091145




      2,0091145




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          7
          down vote



          accepted










          Just start reading from the bottom and print the last word of the first line containing at least "something":



          tac file | awk 'NFprint $NF; exit'


          For example:



          $ cat -vet file # note the spaces and tabs
          hello$
          bla ble bli$
          $
          ^I$
          $ tac file | awk 'NFprint $NF; exit'
          bli


          If you happen to not have tac, just use the same logic when reading the file normally:



          awk 'NFlast=$NF ENDprint last' file


          That is, store the last word whenever there is "something" in a line. Finally, print the stored value.






          share|improve this answer
















          • 2




            tac is the GNU equivalent of Unix tail -r.
            – Stéphane Chazelas
            Jan 3 at 15:35

















          up vote
          1
          down vote













          Keep track of the last word of the last non-empty line and print it at the end:



          awk '/S/ s=$NF; END print(s); '


          No need to keep the whole file in memory to print its lines in reverse order or anything like that.






          share|improve this answer



























            up vote
            0
            down vote













            sed -n '$p' example.txt | awk 'print $NF'


            This will get the last line of the file and display the last column in the last line.






            share|improve this answer






















            • If last line is blank, nothing to print
              – ctac_
              Jan 3 at 17:21










            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%2f414560%2fhow-to-find-the-last-word-in-file-ignore-empty-spaces%23new-answer', 'question_page');

            );

            Post as a guest






























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            7
            down vote



            accepted










            Just start reading from the bottom and print the last word of the first line containing at least "something":



            tac file | awk 'NFprint $NF; exit'


            For example:



            $ cat -vet file # note the spaces and tabs
            hello$
            bla ble bli$
            $
            ^I$
            $ tac file | awk 'NFprint $NF; exit'
            bli


            If you happen to not have tac, just use the same logic when reading the file normally:



            awk 'NFlast=$NF ENDprint last' file


            That is, store the last word whenever there is "something" in a line. Finally, print the stored value.






            share|improve this answer
















            • 2




              tac is the GNU equivalent of Unix tail -r.
              – Stéphane Chazelas
              Jan 3 at 15:35














            up vote
            7
            down vote



            accepted










            Just start reading from the bottom and print the last word of the first line containing at least "something":



            tac file | awk 'NFprint $NF; exit'


            For example:



            $ cat -vet file # note the spaces and tabs
            hello$
            bla ble bli$
            $
            ^I$
            $ tac file | awk 'NFprint $NF; exit'
            bli


            If you happen to not have tac, just use the same logic when reading the file normally:



            awk 'NFlast=$NF ENDprint last' file


            That is, store the last word whenever there is "something" in a line. Finally, print the stored value.






            share|improve this answer
















            • 2




              tac is the GNU equivalent of Unix tail -r.
              – Stéphane Chazelas
              Jan 3 at 15:35












            up vote
            7
            down vote



            accepted







            up vote
            7
            down vote



            accepted






            Just start reading from the bottom and print the last word of the first line containing at least "something":



            tac file | awk 'NFprint $NF; exit'


            For example:



            $ cat -vet file # note the spaces and tabs
            hello$
            bla ble bli$
            $
            ^I$
            $ tac file | awk 'NFprint $NF; exit'
            bli


            If you happen to not have tac, just use the same logic when reading the file normally:



            awk 'NFlast=$NF ENDprint last' file


            That is, store the last word whenever there is "something" in a line. Finally, print the stored value.






            share|improve this answer












            Just start reading from the bottom and print the last word of the first line containing at least "something":



            tac file | awk 'NFprint $NF; exit'


            For example:



            $ cat -vet file # note the spaces and tabs
            hello$
            bla ble bli$
            $
            ^I$
            $ tac file | awk 'NFprint $NF; exit'
            bli


            If you happen to not have tac, just use the same logic when reading the file normally:



            awk 'NFlast=$NF ENDprint last' file


            That is, store the last word whenever there is "something" in a line. Finally, print the stored value.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 3 at 14:52









            fedorqui

            3,89221853




            3,89221853







            • 2




              tac is the GNU equivalent of Unix tail -r.
              – Stéphane Chazelas
              Jan 3 at 15:35












            • 2




              tac is the GNU equivalent of Unix tail -r.
              – Stéphane Chazelas
              Jan 3 at 15:35







            2




            2




            tac is the GNU equivalent of Unix tail -r.
            – Stéphane Chazelas
            Jan 3 at 15:35




            tac is the GNU equivalent of Unix tail -r.
            – Stéphane Chazelas
            Jan 3 at 15:35












            up vote
            1
            down vote













            Keep track of the last word of the last non-empty line and print it at the end:



            awk '/S/ s=$NF; END print(s); '


            No need to keep the whole file in memory to print its lines in reverse order or anything like that.






            share|improve this answer
























              up vote
              1
              down vote













              Keep track of the last word of the last non-empty line and print it at the end:



              awk '/S/ s=$NF; END print(s); '


              No need to keep the whole file in memory to print its lines in reverse order or anything like that.






              share|improve this answer






















                up vote
                1
                down vote










                up vote
                1
                down vote









                Keep track of the last word of the last non-empty line and print it at the end:



                awk '/S/ s=$NF; END print(s); '


                No need to keep the whole file in memory to print its lines in reverse order or anything like that.






                share|improve this answer












                Keep track of the last word of the last non-empty line and print it at the end:



                awk '/S/ s=$NF; END print(s); '


                No need to keep the whole file in memory to print its lines in reverse order or anything like that.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 3 at 17:27









                David Foerster

                915616




                915616




















                    up vote
                    0
                    down vote













                    sed -n '$p' example.txt | awk 'print $NF'


                    This will get the last line of the file and display the last column in the last line.






                    share|improve this answer






















                    • If last line is blank, nothing to print
                      – ctac_
                      Jan 3 at 17:21














                    up vote
                    0
                    down vote













                    sed -n '$p' example.txt | awk 'print $NF'


                    This will get the last line of the file and display the last column in the last line.






                    share|improve this answer






















                    • If last line is blank, nothing to print
                      – ctac_
                      Jan 3 at 17:21












                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    sed -n '$p' example.txt | awk 'print $NF'


                    This will get the last line of the file and display the last column in the last line.






                    share|improve this answer














                    sed -n '$p' example.txt | awk 'print $NF'


                    This will get the last line of the file and display the last column in the last line.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jan 5 at 11:57









                    grg

                    1857




                    1857










                    answered Jan 3 at 15:07









                    Praveen Kumar BS

                    1,010128




                    1,010128











                    • If last line is blank, nothing to print
                      – ctac_
                      Jan 3 at 17:21
















                    • If last line is blank, nothing to print
                      – ctac_
                      Jan 3 at 17:21















                    If last line is blank, nothing to print
                    – ctac_
                    Jan 3 at 17:21




                    If last line is blank, nothing to print
                    – ctac_
                    Jan 3 at 17:21












                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f414560%2fhow-to-find-the-last-word-in-file-ignore-empty-spaces%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?