the \"bookrecords\" directory has multiple files
bookrecords
1.txt
2.txt
3.txt .....
file 2.txt has the c
If you have a deep directory hierarchy below bookrecords
, you could do a
grep -Elsrx '[[:space:]]*author:[[:space:]]*abcd[[:space:]]*' bookrecords
If you only want to look at *.txt files inside bookrecords
, do a
grep -Elx '[[:space:]]*author:[[:space:]]*abcd[[:space:]]*' bookrecords/*.txt
The -l
produces only the file names in the output, and -x
takes care that the regular expression matches the whole line. Hence, a line containing myauthor:abcde would not be selected. Adapt the regexp to your taste, and be careful when replacing abcd by a real author name (it should not contain unescaped regexp characters, such as for instance a period).
If you want to start the line with at least one white space, and have at least one space after the colon:
grep -Elsrx '[[:space:]]+author:[[:space:]]+abcd[[:space:]]*' bookrecords
with awk
$ awk -v search_string="$name" '$0~search_string{print FILENAME; exit}' bookrecords/*
however, I think grep
is better if you're not structurally searching
$ grep -lF "$name" bookrecords/*