How to find and show all the information in a text file

123 Views Asked by At

i'm new in shell programing. I want a text file as my database like this

Booktitle
abcd
Author
abcd
Publisher
abcd
Year of publication
2009

How can i use any keyword and let the program find all the information of that book. For example: When i type grep -i "abcd" books.txt. The program will show:

Booktitle    Author    Publisher    Year of Publication
abcd         abcd      abcd           2009

Thank you guys so much!

1

There are 1 best solutions below

2
On

grep only gives you lines, not groups of lines. You might want to format the file differently. One way is to construct the lines by

id seporator tag seporator data

like so:

abcd --- Title --- abcd
abcd --- Author --- efgh
abcd --- Publisher --- ijkl
abcd --- Year --- 2015

mnop --- Title --- mnop
mnop --- Author --- qrst
mnop --- Publisher --- ijkl
mnop --- Year --- 2007

When you use the book title as your id grep will give you all the information of a book when you search with the title.