User Tools

Site Tools


bash:files:search_files_for_specific_text

BASH - Files - Search files for specific text

Print all lines containing the keyword test:

grep test file.txt

Search with many keywords

Create a file that has all the keywords, one per line and run:

grep -f keywords.txt file.txt

Search using read

Process a file line by line, exit if the line is found:

while read line; do 
  echo $line | grep test && break; 
done < file.txt

Read and Testing return status

while read line; do let c++; echo $line | grep test >/dev/null; 
  if [[ $? == 0 ]] ; then echo Match for line $c : $line;
  else echo No match for line $c; fi  
done < file.txt
bash/files/search_files_for_specific_text.txt · Last modified: 2021/01/26 16:41 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki