For a given string(2 or 3 word) in a .txt file and print the whole line. [duplicate]
Clash 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)
text-processing grep pattern-matching
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.
add a comment |Â
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)
text-processing grep pattern-matching
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.
add a comment |Â
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)
text-processing grep pattern-matching
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
text-processing grep pattern-matching
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.
add a comment |Â
add a comment |Â
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
.
add a comment |Â
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
.
add a comment |Â
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
.
add a comment |Â
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
.
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
.
answered Apr 28 at 16:44
Jesse_b
10.3k22658
10.3k22658
add a comment |Â
add a comment |Â