Below is a snippet of a file named "books.info":
TITLE and AUTHOR ETEXT NO.
Aspects of plant life; with special reference to the British flora, 56900
by Robert Lloyd Praeger
The Vicar of Morwenstow, by Sabine Baring-Gould 56899
[Subtitle: Being a Life of Robert Stephen Hawker, M.A.]
Raamatun tutkisteluja IV, mennessä Charles T. Russell 56898
[Subtitle: Harmagedonin taistelu]
[Language: Finnish]
Tom Thatcher's Fortune, by Horatio Alger, Jr. 56896
A Yankee Flier in the Far East, by Al Avery 56895
and George Rutherford Montgomery
[Illustrator: Paul Laune]
Nancy Brandon's Mystery, by Lillian Garis 56894
The Junior Classics, Volume 3: Tales from Greece and Rome, by Various 56887
~ ~ ~ ~ Posting Dates for the below eBooks: 1 Mar 2018 to 31 Mar 2018 ~ ~ ~ ~
TITLE and AUTHOR ETEXT NO.
The American Missionary, Volume 41, No. 1, January, 1887, by Various 56886
Morganin miljoonat, mennessä Sven Elvestad 56885
[Author a.k.a. Stein Riverton]
[Subtitle: Salapoliisiromaani]
[Language: Finnish]
"Trip to the Sunny South" in March, 1885, by L. S. D 56884
Balaam and His Master, by Joel Chandler Harris 56883
[Subtitle: and Other Sketches and Stories]
I am trying to get the full info for a book if I search it by the author name.
Example1:
Search Keyword: Al Avery
Then return should be:
A Yankee Flier in the Far East, by Al Avery 56895
and George Rutherford Montgomery
[Illustrator: Paul Laune]
Example2:
Search Keyword: Robert Lloyd Praeger
Then return should be:
Aspects of plant life; with special reference to the British flora, 56900
by Robert Lloyd Praeger
I have tried this:
#!/bin/bash
if [ -f books.info ]
then
echo Please enter search keyword:
read keyword
command=$(grep -i "$keyword" books.info)
echo $command
else
echo books.info file is missing
fi
But it is not working exactly they way I want it to. Could anyone please help me with this?
EDIT
Updated the file structure after noticing a date line. Tried: awk -v RS='\n\n' "/${keyword}/" infile and: awk -v RS= -v keyword="$keyword" '$0 ~ keyword' file.txt As suggested by the answers given below by @andlrc and @αғsнιη but it prints out a lot of other stuffs which are not related. My best guess is this line is breaking it:
~ ~ ~ ~ Posting Dates for the below eBooks: 1 Mar 2018 to 31 Mar 2018 ~ ~ ~ ~
Because the top of this line has 2 spaces and below one space, I think because of the top two spaces its not working properly.