I am using "grep" in a while loop (to read line by line) a long document (this long file contain multiple paragraphs,each paragraph starts with a date/time).
The purpose is to find certain phrase and print or echo the paragraph's date/time beside that phrase. How can I "echo" or "print" the letters "NULL" when grep can't capture the phrase. For example:
Let's say we have these three paragraph in the file (test.txt):
20170101,05:00 AM, I am using grep to read certain phrase1 in a long document. 20170102,09:30 AM, I am using grep to read certain phrase2 in a long document. 20170103,05:30 AM, I am using grep to read page in a long document.
How can I echo or print a "certain phrase" and date time and if "certain phrase not available replace the output by NULL so the output as follow:
20170101,05:00 AM,certain phrase1 20170102,09:30 AM,certain phrase2 20170103,05:30 AM,NULL . . .
I use the following:
while read -r line; do date=$(grep -c "201*") phrase=$(grep -Eo "certain phrase") echo $date,$phrase done < test.txt