For a given input search the .txt file and if the search found print the whole line

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











up vote
-1
down vote

favorite












Input File My .txt file contains these kind of content :



Tom Thatcher's Fortune, by Horatio Alger, Jr. 56896
Paradise Lost, by John Milton 20


The first string is book name,second is author and the last one is book number. I want to print the whole line when someone search for the book number (if the book number matches).
I'm new to bash scripting.







share|improve this question

























    up vote
    -1
    down vote

    favorite












    Input File My .txt file contains these kind of content :



    Tom Thatcher's Fortune, by Horatio Alger, Jr. 56896
    Paradise Lost, by John Milton 20


    The first string is book name,second is author and the last one is book number. I want to print the whole line when someone search for the book number (if the book number matches).
    I'm new to bash scripting.







    share|improve this question























      up vote
      -1
      down vote

      favorite









      up vote
      -1
      down vote

      favorite











      Input File My .txt file contains these kind of content :



      Tom Thatcher's Fortune, by Horatio Alger, Jr. 56896
      Paradise Lost, by John Milton 20


      The first string is book name,second is author and the last one is book number. I want to print the whole line when someone search for the book number (if the book number matches).
      I'm new to bash scripting.







      share|improve this question













      Input File My .txt file contains these kind of content :



      Tom Thatcher's Fortune, by Horatio Alger, Jr. 56896
      Paradise Lost, by John Milton 20


      The first string is book name,second is author and the last one is book number. I want to print the whole line when someone search for the book number (if the book number matches).
      I'm new to bash scripting.









      share|improve this question












      share|improve this question




      share|improve this question








      edited Apr 28 at 15:00
























      asked Apr 27 at 15:21









      Nazmus Shakib

      32




      32




















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          Something like



          grep BOOKNUMBER textfile.txt


          should solve this.



          If you want to avoid that searching for book number 96 also lists Tom Thatcher's Fortune you can use



          grep '[[:space:]]BOOKNUMBER[[:space:]]*$' textfile.txt


          instead.




          As a script (save as searchbook)



          #!/bin/bash

          BOOKFILE=/path/to/bookfile.txt

          BOOK=$1
          if [[ -z "$BOOK" ]]; then
          echo -n "Booknumber? "
          read BOOK
          fi

          grep '[[:space:]]'$BOOK'[[:space:]]*$' $BOOKFILE


          You can then either run



          searchbook BOOKNUMBER


          or



          searchbook


          In the second case you will get prompted for the number.






          share|improve this answer























          • Can you please solve it in script? I'm kind of confused of grep as I never work with it.Thanks
            – Nazmus Shakib
            Apr 27 at 15:41










          • Just type it into a bash prompt, it won't break anything.
            – nohillside
            Apr 27 at 15:44










          • It works but it also print some other lines containing 20.I can differ those with my desired line is whitespace.There is some whitespace before the book number and I only want to print the line when it matches with the book number not some other line.How can I do that.You can take a look on my input file.I give link in above.Thanks sir.
            – Nazmus Shakib
            Apr 27 at 16:00










          • I add 2 more space in the grep and it works fine now.Thanks
            – Nazmus Shakib
            Apr 27 at 16:04










          • btw What the *$ sign means.If I want to search for a string what should I change in the grep line.Thanks
            – Nazmus Shakib
            Apr 27 at 16:11

















          up vote
          0
          down vote













          You can use grep for this:



          grep 56896 file.txt


          If you wanted to script it, it could be something like this:



          #!/bin/bash

          inputfile='/path/to/file.txt'

          grep "$1" "$inputfile"


          Which you would then run like:



          $ ./script.sh 56896





          share|improve this answer





















          • I want to take input from user and then search the file with the input and if there is any match then print the whole line.My input file contains a lot of strings and integers.
            – Nazmus Shakib
            Apr 27 at 15:39










          • @NazmusShakib: That is what the script would do
            – Jesse_b
            Apr 27 at 15:44

















          up vote
          0
          down vote













          To avoid catching book # 196 along with book # 96, I suggest a similar but more terse usage of grep:



          grep ' 96$' file.txt


          The $ symbol in regular expressions meaning 'the end of the text sample', so it will only match on the correct line.



          This can also be put into a script:



          #!/bin/bash

          grep " $1$" /path/to/file.txt


          This can also be done a bit more safely with awk:



          awk -v book=96 '$NF == book print' file.txt





          share|improve this answer




























            up vote
            0
            down vote













            To print the lines for which the last word is something, awk comes to mind:



            awk '$NF == "123"' # string comparison
            awk '$NF == 123' # number comparison, would also match on 0123, 123.0, 1.23e2
            awk '$NF == ENVIRON["ENVVAR"]' # number comparison if both $NF and $ENVVAR
            # look like numbers, string otherwise


            For awk, those words by default are blank-separated. Depending on the implementation blank may be only space or tab characters, some may include other Unicode blank characters if classified as such in the locale. The busybox awk implementation also includes vertical spacing characters including the carriage return character.



            If your file is formatted with MS-DOS line delimiters, that is the CR LF sequence of characters as opposed to just LF, then except with busybox awk, the above won't work. And even with busybox awk, the output lines would still have those problematic control characters that shouldn't be there on Unix.



            So you should preprocess the input with dos2unix or d2u to convert the line endings to Unix format.



            < file.dos dos2unix | awk '$NF == 123'


            Your input file looks like it's made of records that are separated by blank lines. If you want to print the full records for a book number, you could do:



            export BOOKNUMBER=123
            < file.dos dos2unix | awk -v RS= -F 'n' '
            field[split($1, field, " ")] == ENVIRON["BOOKNUMBER"]'


            There, we set the record separator to the empty string to enter the paragraph mode (records separated by blank lines), and the field separator to newline (aka LF) so the first line of each record is in $1 (the first field). We split that first line with the special " " field separator which is the default word splitting into the field array. And then compare the last element of that array (split() returns the number of elements, so field[split()] is the last element) with the content of the $BOOKNUMBER environment variable.






            share|improve this answer























            • awk '$NF == 123' is OK with GNU Awk 4.1.4 if the line end with crlf
              – ctac_
              Apr 27 at 16:32










            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%2f440450%2ffor-a-given-input-search-the-txt-file-and-if-the-search-found-print-the-whole-l%23new-answer', 'question_page');

            );

            Post as a guest






























            4 Answers
            4






            active

            oldest

            votes








            4 Answers
            4






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            Something like



            grep BOOKNUMBER textfile.txt


            should solve this.



            If you want to avoid that searching for book number 96 also lists Tom Thatcher's Fortune you can use



            grep '[[:space:]]BOOKNUMBER[[:space:]]*$' textfile.txt


            instead.




            As a script (save as searchbook)



            #!/bin/bash

            BOOKFILE=/path/to/bookfile.txt

            BOOK=$1
            if [[ -z "$BOOK" ]]; then
            echo -n "Booknumber? "
            read BOOK
            fi

            grep '[[:space:]]'$BOOK'[[:space:]]*$' $BOOKFILE


            You can then either run



            searchbook BOOKNUMBER


            or



            searchbook


            In the second case you will get prompted for the number.






            share|improve this answer























            • Can you please solve it in script? I'm kind of confused of grep as I never work with it.Thanks
              – Nazmus Shakib
              Apr 27 at 15:41










            • Just type it into a bash prompt, it won't break anything.
              – nohillside
              Apr 27 at 15:44










            • It works but it also print some other lines containing 20.I can differ those with my desired line is whitespace.There is some whitespace before the book number and I only want to print the line when it matches with the book number not some other line.How can I do that.You can take a look on my input file.I give link in above.Thanks sir.
              – Nazmus Shakib
              Apr 27 at 16:00










            • I add 2 more space in the grep and it works fine now.Thanks
              – Nazmus Shakib
              Apr 27 at 16:04










            • btw What the *$ sign means.If I want to search for a string what should I change in the grep line.Thanks
              – Nazmus Shakib
              Apr 27 at 16:11














            up vote
            0
            down vote



            accepted










            Something like



            grep BOOKNUMBER textfile.txt


            should solve this.



            If you want to avoid that searching for book number 96 also lists Tom Thatcher's Fortune you can use



            grep '[[:space:]]BOOKNUMBER[[:space:]]*$' textfile.txt


            instead.




            As a script (save as searchbook)



            #!/bin/bash

            BOOKFILE=/path/to/bookfile.txt

            BOOK=$1
            if [[ -z "$BOOK" ]]; then
            echo -n "Booknumber? "
            read BOOK
            fi

            grep '[[:space:]]'$BOOK'[[:space:]]*$' $BOOKFILE


            You can then either run



            searchbook BOOKNUMBER


            or



            searchbook


            In the second case you will get prompted for the number.






            share|improve this answer























            • Can you please solve it in script? I'm kind of confused of grep as I never work with it.Thanks
              – Nazmus Shakib
              Apr 27 at 15:41










            • Just type it into a bash prompt, it won't break anything.
              – nohillside
              Apr 27 at 15:44










            • It works but it also print some other lines containing 20.I can differ those with my desired line is whitespace.There is some whitespace before the book number and I only want to print the line when it matches with the book number not some other line.How can I do that.You can take a look on my input file.I give link in above.Thanks sir.
              – Nazmus Shakib
              Apr 27 at 16:00










            • I add 2 more space in the grep and it works fine now.Thanks
              – Nazmus Shakib
              Apr 27 at 16:04










            • btw What the *$ sign means.If I want to search for a string what should I change in the grep line.Thanks
              – Nazmus Shakib
              Apr 27 at 16:11












            up vote
            0
            down vote



            accepted







            up vote
            0
            down vote



            accepted






            Something like



            grep BOOKNUMBER textfile.txt


            should solve this.



            If you want to avoid that searching for book number 96 also lists Tom Thatcher's Fortune you can use



            grep '[[:space:]]BOOKNUMBER[[:space:]]*$' textfile.txt


            instead.




            As a script (save as searchbook)



            #!/bin/bash

            BOOKFILE=/path/to/bookfile.txt

            BOOK=$1
            if [[ -z "$BOOK" ]]; then
            echo -n "Booknumber? "
            read BOOK
            fi

            grep '[[:space:]]'$BOOK'[[:space:]]*$' $BOOKFILE


            You can then either run



            searchbook BOOKNUMBER


            or



            searchbook


            In the second case you will get prompted for the number.






            share|improve this answer















            Something like



            grep BOOKNUMBER textfile.txt


            should solve this.



            If you want to avoid that searching for book number 96 also lists Tom Thatcher's Fortune you can use



            grep '[[:space:]]BOOKNUMBER[[:space:]]*$' textfile.txt


            instead.




            As a script (save as searchbook)



            #!/bin/bash

            BOOKFILE=/path/to/bookfile.txt

            BOOK=$1
            if [[ -z "$BOOK" ]]; then
            echo -n "Booknumber? "
            read BOOK
            fi

            grep '[[:space:]]'$BOOK'[[:space:]]*$' $BOOKFILE


            You can then either run



            searchbook BOOKNUMBER


            or



            searchbook


            In the second case you will get prompted for the number.







            share|improve this answer















            share|improve this answer



            share|improve this answer








            edited Apr 27 at 15:48


























            answered Apr 27 at 15:24









            nohillside

            1,858616




            1,858616











            • Can you please solve it in script? I'm kind of confused of grep as I never work with it.Thanks
              – Nazmus Shakib
              Apr 27 at 15:41










            • Just type it into a bash prompt, it won't break anything.
              – nohillside
              Apr 27 at 15:44










            • It works but it also print some other lines containing 20.I can differ those with my desired line is whitespace.There is some whitespace before the book number and I only want to print the line when it matches with the book number not some other line.How can I do that.You can take a look on my input file.I give link in above.Thanks sir.
              – Nazmus Shakib
              Apr 27 at 16:00










            • I add 2 more space in the grep and it works fine now.Thanks
              – Nazmus Shakib
              Apr 27 at 16:04










            • btw What the *$ sign means.If I want to search for a string what should I change in the grep line.Thanks
              – Nazmus Shakib
              Apr 27 at 16:11
















            • Can you please solve it in script? I'm kind of confused of grep as I never work with it.Thanks
              – Nazmus Shakib
              Apr 27 at 15:41










            • Just type it into a bash prompt, it won't break anything.
              – nohillside
              Apr 27 at 15:44










            • It works but it also print some other lines containing 20.I can differ those with my desired line is whitespace.There is some whitespace before the book number and I only want to print the line when it matches with the book number not some other line.How can I do that.You can take a look on my input file.I give link in above.Thanks sir.
              – Nazmus Shakib
              Apr 27 at 16:00










            • I add 2 more space in the grep and it works fine now.Thanks
              – Nazmus Shakib
              Apr 27 at 16:04










            • btw What the *$ sign means.If I want to search for a string what should I change in the grep line.Thanks
              – Nazmus Shakib
              Apr 27 at 16:11















            Can you please solve it in script? I'm kind of confused of grep as I never work with it.Thanks
            – Nazmus Shakib
            Apr 27 at 15:41




            Can you please solve it in script? I'm kind of confused of grep as I never work with it.Thanks
            – Nazmus Shakib
            Apr 27 at 15:41












            Just type it into a bash prompt, it won't break anything.
            – nohillside
            Apr 27 at 15:44




            Just type it into a bash prompt, it won't break anything.
            – nohillside
            Apr 27 at 15:44












            It works but it also print some other lines containing 20.I can differ those with my desired line is whitespace.There is some whitespace before the book number and I only want to print the line when it matches with the book number not some other line.How can I do that.You can take a look on my input file.I give link in above.Thanks sir.
            – Nazmus Shakib
            Apr 27 at 16:00




            It works but it also print some other lines containing 20.I can differ those with my desired line is whitespace.There is some whitespace before the book number and I only want to print the line when it matches with the book number not some other line.How can I do that.You can take a look on my input file.I give link in above.Thanks sir.
            – Nazmus Shakib
            Apr 27 at 16:00












            I add 2 more space in the grep and it works fine now.Thanks
            – Nazmus Shakib
            Apr 27 at 16:04




            I add 2 more space in the grep and it works fine now.Thanks
            – Nazmus Shakib
            Apr 27 at 16:04












            btw What the *$ sign means.If I want to search for a string what should I change in the grep line.Thanks
            – Nazmus Shakib
            Apr 27 at 16:11




            btw What the *$ sign means.If I want to search for a string what should I change in the grep line.Thanks
            – Nazmus Shakib
            Apr 27 at 16:11












            up vote
            0
            down vote













            You can use grep for this:



            grep 56896 file.txt


            If you wanted to script it, it could be something like this:



            #!/bin/bash

            inputfile='/path/to/file.txt'

            grep "$1" "$inputfile"


            Which you would then run like:



            $ ./script.sh 56896





            share|improve this answer





















            • I want to take input from user and then search the file with the input and if there is any match then print the whole line.My input file contains a lot of strings and integers.
              – Nazmus Shakib
              Apr 27 at 15:39










            • @NazmusShakib: That is what the script would do
              – Jesse_b
              Apr 27 at 15:44














            up vote
            0
            down vote













            You can use grep for this:



            grep 56896 file.txt


            If you wanted to script it, it could be something like this:



            #!/bin/bash

            inputfile='/path/to/file.txt'

            grep "$1" "$inputfile"


            Which you would then run like:



            $ ./script.sh 56896





            share|improve this answer





















            • I want to take input from user and then search the file with the input and if there is any match then print the whole line.My input file contains a lot of strings and integers.
              – Nazmus Shakib
              Apr 27 at 15:39










            • @NazmusShakib: That is what the script would do
              – Jesse_b
              Apr 27 at 15:44












            up vote
            0
            down vote










            up vote
            0
            down vote









            You can use grep for this:



            grep 56896 file.txt


            If you wanted to script it, it could be something like this:



            #!/bin/bash

            inputfile='/path/to/file.txt'

            grep "$1" "$inputfile"


            Which you would then run like:



            $ ./script.sh 56896





            share|improve this answer













            You can use grep for this:



            grep 56896 file.txt


            If you wanted to script it, it could be something like this:



            #!/bin/bash

            inputfile='/path/to/file.txt'

            grep "$1" "$inputfile"


            Which you would then run like:



            $ ./script.sh 56896






            share|improve this answer













            share|improve this answer



            share|improve this answer











            answered Apr 27 at 15:23









            Jesse_b

            10.3k22658




            10.3k22658











            • I want to take input from user and then search the file with the input and if there is any match then print the whole line.My input file contains a lot of strings and integers.
              – Nazmus Shakib
              Apr 27 at 15:39










            • @NazmusShakib: That is what the script would do
              – Jesse_b
              Apr 27 at 15:44
















            • I want to take input from user and then search the file with the input and if there is any match then print the whole line.My input file contains a lot of strings and integers.
              – Nazmus Shakib
              Apr 27 at 15:39










            • @NazmusShakib: That is what the script would do
              – Jesse_b
              Apr 27 at 15:44















            I want to take input from user and then search the file with the input and if there is any match then print the whole line.My input file contains a lot of strings and integers.
            – Nazmus Shakib
            Apr 27 at 15:39




            I want to take input from user and then search the file with the input and if there is any match then print the whole line.My input file contains a lot of strings and integers.
            – Nazmus Shakib
            Apr 27 at 15:39












            @NazmusShakib: That is what the script would do
            – Jesse_b
            Apr 27 at 15:44




            @NazmusShakib: That is what the script would do
            – Jesse_b
            Apr 27 at 15:44










            up vote
            0
            down vote













            To avoid catching book # 196 along with book # 96, I suggest a similar but more terse usage of grep:



            grep ' 96$' file.txt


            The $ symbol in regular expressions meaning 'the end of the text sample', so it will only match on the correct line.



            This can also be put into a script:



            #!/bin/bash

            grep " $1$" /path/to/file.txt


            This can also be done a bit more safely with awk:



            awk -v book=96 '$NF == book print' file.txt





            share|improve this answer

























              up vote
              0
              down vote













              To avoid catching book # 196 along with book # 96, I suggest a similar but more terse usage of grep:



              grep ' 96$' file.txt


              The $ symbol in regular expressions meaning 'the end of the text sample', so it will only match on the correct line.



              This can also be put into a script:



              #!/bin/bash

              grep " $1$" /path/to/file.txt


              This can also be done a bit more safely with awk:



              awk -v book=96 '$NF == book print' file.txt





              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                To avoid catching book # 196 along with book # 96, I suggest a similar but more terse usage of grep:



                grep ' 96$' file.txt


                The $ symbol in regular expressions meaning 'the end of the text sample', so it will only match on the correct line.



                This can also be put into a script:



                #!/bin/bash

                grep " $1$" /path/to/file.txt


                This can also be done a bit more safely with awk:



                awk -v book=96 '$NF == book print' file.txt





                share|improve this answer













                To avoid catching book # 196 along with book # 96, I suggest a similar but more terse usage of grep:



                grep ' 96$' file.txt


                The $ symbol in regular expressions meaning 'the end of the text sample', so it will only match on the correct line.



                This can also be put into a script:



                #!/bin/bash

                grep " $1$" /path/to/file.txt


                This can also be done a bit more safely with awk:



                awk -v book=96 '$NF == book print' file.txt






                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Apr 27 at 15:39









                DopeGhoti

                40k54779




                40k54779




















                    up vote
                    0
                    down vote













                    To print the lines for which the last word is something, awk comes to mind:



                    awk '$NF == "123"' # string comparison
                    awk '$NF == 123' # number comparison, would also match on 0123, 123.0, 1.23e2
                    awk '$NF == ENVIRON["ENVVAR"]' # number comparison if both $NF and $ENVVAR
                    # look like numbers, string otherwise


                    For awk, those words by default are blank-separated. Depending on the implementation blank may be only space or tab characters, some may include other Unicode blank characters if classified as such in the locale. The busybox awk implementation also includes vertical spacing characters including the carriage return character.



                    If your file is formatted with MS-DOS line delimiters, that is the CR LF sequence of characters as opposed to just LF, then except with busybox awk, the above won't work. And even with busybox awk, the output lines would still have those problematic control characters that shouldn't be there on Unix.



                    So you should preprocess the input with dos2unix or d2u to convert the line endings to Unix format.



                    < file.dos dos2unix | awk '$NF == 123'


                    Your input file looks like it's made of records that are separated by blank lines. If you want to print the full records for a book number, you could do:



                    export BOOKNUMBER=123
                    < file.dos dos2unix | awk -v RS= -F 'n' '
                    field[split($1, field, " ")] == ENVIRON["BOOKNUMBER"]'


                    There, we set the record separator to the empty string to enter the paragraph mode (records separated by blank lines), and the field separator to newline (aka LF) so the first line of each record is in $1 (the first field). We split that first line with the special " " field separator which is the default word splitting into the field array. And then compare the last element of that array (split() returns the number of elements, so field[split()] is the last element) with the content of the $BOOKNUMBER environment variable.






                    share|improve this answer























                    • awk '$NF == 123' is OK with GNU Awk 4.1.4 if the line end with crlf
                      – ctac_
                      Apr 27 at 16:32














                    up vote
                    0
                    down vote













                    To print the lines for which the last word is something, awk comes to mind:



                    awk '$NF == "123"' # string comparison
                    awk '$NF == 123' # number comparison, would also match on 0123, 123.0, 1.23e2
                    awk '$NF == ENVIRON["ENVVAR"]' # number comparison if both $NF and $ENVVAR
                    # look like numbers, string otherwise


                    For awk, those words by default are blank-separated. Depending on the implementation blank may be only space or tab characters, some may include other Unicode blank characters if classified as such in the locale. The busybox awk implementation also includes vertical spacing characters including the carriage return character.



                    If your file is formatted with MS-DOS line delimiters, that is the CR LF sequence of characters as opposed to just LF, then except with busybox awk, the above won't work. And even with busybox awk, the output lines would still have those problematic control characters that shouldn't be there on Unix.



                    So you should preprocess the input with dos2unix or d2u to convert the line endings to Unix format.



                    < file.dos dos2unix | awk '$NF == 123'


                    Your input file looks like it's made of records that are separated by blank lines. If you want to print the full records for a book number, you could do:



                    export BOOKNUMBER=123
                    < file.dos dos2unix | awk -v RS= -F 'n' '
                    field[split($1, field, " ")] == ENVIRON["BOOKNUMBER"]'


                    There, we set the record separator to the empty string to enter the paragraph mode (records separated by blank lines), and the field separator to newline (aka LF) so the first line of each record is in $1 (the first field). We split that first line with the special " " field separator which is the default word splitting into the field array. And then compare the last element of that array (split() returns the number of elements, so field[split()] is the last element) with the content of the $BOOKNUMBER environment variable.






                    share|improve this answer























                    • awk '$NF == 123' is OK with GNU Awk 4.1.4 if the line end with crlf
                      – ctac_
                      Apr 27 at 16:32












                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    To print the lines for which the last word is something, awk comes to mind:



                    awk '$NF == "123"' # string comparison
                    awk '$NF == 123' # number comparison, would also match on 0123, 123.0, 1.23e2
                    awk '$NF == ENVIRON["ENVVAR"]' # number comparison if both $NF and $ENVVAR
                    # look like numbers, string otherwise


                    For awk, those words by default are blank-separated. Depending on the implementation blank may be only space or tab characters, some may include other Unicode blank characters if classified as such in the locale. The busybox awk implementation also includes vertical spacing characters including the carriage return character.



                    If your file is formatted with MS-DOS line delimiters, that is the CR LF sequence of characters as opposed to just LF, then except with busybox awk, the above won't work. And even with busybox awk, the output lines would still have those problematic control characters that shouldn't be there on Unix.



                    So you should preprocess the input with dos2unix or d2u to convert the line endings to Unix format.



                    < file.dos dos2unix | awk '$NF == 123'


                    Your input file looks like it's made of records that are separated by blank lines. If you want to print the full records for a book number, you could do:



                    export BOOKNUMBER=123
                    < file.dos dos2unix | awk -v RS= -F 'n' '
                    field[split($1, field, " ")] == ENVIRON["BOOKNUMBER"]'


                    There, we set the record separator to the empty string to enter the paragraph mode (records separated by blank lines), and the field separator to newline (aka LF) so the first line of each record is in $1 (the first field). We split that first line with the special " " field separator which is the default word splitting into the field array. And then compare the last element of that array (split() returns the number of elements, so field[split()] is the last element) with the content of the $BOOKNUMBER environment variable.






                    share|improve this answer















                    To print the lines for which the last word is something, awk comes to mind:



                    awk '$NF == "123"' # string comparison
                    awk '$NF == 123' # number comparison, would also match on 0123, 123.0, 1.23e2
                    awk '$NF == ENVIRON["ENVVAR"]' # number comparison if both $NF and $ENVVAR
                    # look like numbers, string otherwise


                    For awk, those words by default are blank-separated. Depending on the implementation blank may be only space or tab characters, some may include other Unicode blank characters if classified as such in the locale. The busybox awk implementation also includes vertical spacing characters including the carriage return character.



                    If your file is formatted with MS-DOS line delimiters, that is the CR LF sequence of characters as opposed to just LF, then except with busybox awk, the above won't work. And even with busybox awk, the output lines would still have those problematic control characters that shouldn't be there on Unix.



                    So you should preprocess the input with dos2unix or d2u to convert the line endings to Unix format.



                    < file.dos dos2unix | awk '$NF == 123'


                    Your input file looks like it's made of records that are separated by blank lines. If you want to print the full records for a book number, you could do:



                    export BOOKNUMBER=123
                    < file.dos dos2unix | awk -v RS= -F 'n' '
                    field[split($1, field, " ")] == ENVIRON["BOOKNUMBER"]'


                    There, we set the record separator to the empty string to enter the paragraph mode (records separated by blank lines), and the field separator to newline (aka LF) so the first line of each record is in $1 (the first field). We split that first line with the special " " field separator which is the default word splitting into the field array. And then compare the last element of that array (split() returns the number of elements, so field[split()] is the last element) with the content of the $BOOKNUMBER environment variable.







                    share|improve this answer















                    share|improve this answer



                    share|improve this answer








                    edited Apr 27 at 16:21


























                    answered Apr 27 at 15:43









                    Stéphane Chazelas

                    279k53514846




                    279k53514846











                    • awk '$NF == 123' is OK with GNU Awk 4.1.4 if the line end with crlf
                      – ctac_
                      Apr 27 at 16:32
















                    • awk '$NF == 123' is OK with GNU Awk 4.1.4 if the line end with crlf
                      – ctac_
                      Apr 27 at 16:32















                    awk '$NF == 123' is OK with GNU Awk 4.1.4 if the line end with crlf
                    – ctac_
                    Apr 27 at 16:32




                    awk '$NF == 123' is OK with GNU Awk 4.1.4 if the line end with crlf
                    – ctac_
                    Apr 27 at 16:32












                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f440450%2ffor-a-given-input-search-the-txt-file-and-if-the-search-found-print-the-whole-l%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?

                    Bahrain

                    Postfix configuration issue with fips on centos 7; mailgun relay