For a given string(2 or 3 word) in a .txt file and print the whole line. [duplicate]

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











up vote
0
down vote

favorite













This question already has an answer here:



  • Why does my shell script choke on whitespace or other special characters?

    4 answers



My .txt file contains these kind of content :



The Law and Lawyers of Pickwick, by Frank Lockwood 21214
Frankenstein, by Mary Wollstonecraft 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 with the author name.I tried using grep.



#!/bin/bash
BOOKFILE="/home/sk/GUTINDEX.ALL"
author=$1
if [[ -z "$author" ]]; then
echo -n "Author name : "
read author
fi
grep $author $BOOKFILE


If I run this and search for Frank Lockwood it prints both the lines.But I want to print the line when the entire input string match(both the first name and last name)







share|improve this question











marked as duplicate by Jesse_b, Community♦ Apr 28 at 16:45


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















    up vote
    0
    down vote

    favorite













    This question already has an answer here:



    • Why does my shell script choke on whitespace or other special characters?

      4 answers



    My .txt file contains these kind of content :



    The Law and Lawyers of Pickwick, by Frank Lockwood 21214
    Frankenstein, by Mary Wollstonecraft 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 with the author name.I tried using grep.



    #!/bin/bash
    BOOKFILE="/home/sk/GUTINDEX.ALL"
    author=$1
    if [[ -z "$author" ]]; then
    echo -n "Author name : "
    read author
    fi
    grep $author $BOOKFILE


    If I run this and search for Frank Lockwood it prints both the lines.But I want to print the line when the entire input string match(both the first name and last name)







    share|improve this question











    marked as duplicate by Jesse_b, Community♦ Apr 28 at 16:45


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite












      This question already has an answer here:



      • Why does my shell script choke on whitespace or other special characters?

        4 answers



      My .txt file contains these kind of content :



      The Law and Lawyers of Pickwick, by Frank Lockwood 21214
      Frankenstein, by Mary Wollstonecraft 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 with the author name.I tried using grep.



      #!/bin/bash
      BOOKFILE="/home/sk/GUTINDEX.ALL"
      author=$1
      if [[ -z "$author" ]]; then
      echo -n "Author name : "
      read author
      fi
      grep $author $BOOKFILE


      If I run this and search for Frank Lockwood it prints both the lines.But I want to print the line when the entire input string match(both the first name and last name)







      share|improve this question












      This question already has an answer here:



      • Why does my shell script choke on whitespace or other special characters?

        4 answers



      My .txt file contains these kind of content :



      The Law and Lawyers of Pickwick, by Frank Lockwood 21214
      Frankenstein, by Mary Wollstonecraft 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 with the author name.I tried using grep.



      #!/bin/bash
      BOOKFILE="/home/sk/GUTINDEX.ALL"
      author=$1
      if [[ -z "$author" ]]; then
      echo -n "Author name : "
      read author
      fi
      grep $author $BOOKFILE


      If I run this and search for Frank Lockwood it prints both the lines.But I want to print the line when the entire input string match(both the first name and last name)





      This question already has an answer here:



      • Why does my shell script choke on whitespace or other special characters?

        4 answers









      share|improve this question










      share|improve this question




      share|improve this question









      asked Apr 28 at 16:33









      Nazmus Shakib

      32




      32




      marked as duplicate by Jesse_b, Community♦ Apr 28 at 16:45


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by Jesse_b, Community♦ Apr 28 at 16:45


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          -1
          down vote



          accepted










          You should change it to this:



          #!/bin/bash
          BOOKFILE="/home/sk/GUTINDEX.ALL"
          author=$@
          if [[ -z "$author" ]]; then
          read -rp "Author name: " author
          fi
          grep "$author" "$BOOKFILE"


          If you run it as:



          $ ./script Frank Lockwood


          You are setting positional parameter 1 to Frank and parameter 2 to Lockwood. Parameter 2 is not in use in your script. $@ is an array that will represent all positional parameters.



          Also if you are to allow read to set your author variable to a string with whitespace it will fail on the grep line without quoting author.






          share|improve this answer




























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            -1
            down vote



            accepted










            You should change it to this:



            #!/bin/bash
            BOOKFILE="/home/sk/GUTINDEX.ALL"
            author=$@
            if [[ -z "$author" ]]; then
            read -rp "Author name: " author
            fi
            grep "$author" "$BOOKFILE"


            If you run it as:



            $ ./script Frank Lockwood


            You are setting positional parameter 1 to Frank and parameter 2 to Lockwood. Parameter 2 is not in use in your script. $@ is an array that will represent all positional parameters.



            Also if you are to allow read to set your author variable to a string with whitespace it will fail on the grep line without quoting author.






            share|improve this answer

























              up vote
              -1
              down vote



              accepted










              You should change it to this:



              #!/bin/bash
              BOOKFILE="/home/sk/GUTINDEX.ALL"
              author=$@
              if [[ -z "$author" ]]; then
              read -rp "Author name: " author
              fi
              grep "$author" "$BOOKFILE"


              If you run it as:



              $ ./script Frank Lockwood


              You are setting positional parameter 1 to Frank and parameter 2 to Lockwood. Parameter 2 is not in use in your script. $@ is an array that will represent all positional parameters.



              Also if you are to allow read to set your author variable to a string with whitespace it will fail on the grep line without quoting author.






              share|improve this answer























                up vote
                -1
                down vote



                accepted







                up vote
                -1
                down vote



                accepted






                You should change it to this:



                #!/bin/bash
                BOOKFILE="/home/sk/GUTINDEX.ALL"
                author=$@
                if [[ -z "$author" ]]; then
                read -rp "Author name: " author
                fi
                grep "$author" "$BOOKFILE"


                If you run it as:



                $ ./script Frank Lockwood


                You are setting positional parameter 1 to Frank and parameter 2 to Lockwood. Parameter 2 is not in use in your script. $@ is an array that will represent all positional parameters.



                Also if you are to allow read to set your author variable to a string with whitespace it will fail on the grep line without quoting author.






                share|improve this answer













                You should change it to this:



                #!/bin/bash
                BOOKFILE="/home/sk/GUTINDEX.ALL"
                author=$@
                if [[ -z "$author" ]]; then
                read -rp "Author name: " author
                fi
                grep "$author" "$BOOKFILE"


                If you run it as:



                $ ./script Frank Lockwood


                You are setting positional parameter 1 to Frank and parameter 2 to Lockwood. Parameter 2 is not in use in your script. $@ is an array that will represent all positional parameters.



                Also if you are to allow read to set your author variable to a string with whitespace it will fail on the grep line without quoting author.







                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Apr 28 at 16:44









                Jesse_b

                10.3k22658




                10.3k22658












                    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?