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; dodate=$(grep -c 201*)phrase=$(grep  -Eo certain phrase)echo $date,$phrasedone < test.txt
replace the empty fields from the output of grep with a string
text processing;grep
while read -r line; do  date=$(grep -Eo  201.{12}.M, <<< $line)  phrase=$(grep  -Eo certain phrase <<< $line)  echo ${date}${phrase:-NULL}done < test.txtYou were reading the line, but not grepping against it. I tweaked the regex on the first grep to match the date part (instead of -c counting it). The last piece to the puzzle was to echo the $phrase variable with parameter expansion to replace an empty value with the word NULL.