bash:files:search_files_for_specific_text
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
bash:files:search_files_for_specific_text [2021/01/26 16:36] – created peter | bash:files:search_files_for_specific_text [2021/01/26 16:41] (current) – peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== BASH - Files - Search files for specific text ====== | ====== BASH - Files - Search files for specific text ====== | ||
+ | |||
+ | ===== Simple Search ===== | ||
+ | |||
+ | Print all lines containing the keyword test: | ||
+ | | ||
+ | <code bash> | ||
+ | grep test file.txt | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Search with many keywords ===== | ||
+ | |||
+ | Create a file that has all the keywords, one per line and run: | ||
+ | |||
+ | <code bash> | ||
+ | grep -f keywords.txt file.txt | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Search using read ===== | ||
+ | |||
+ | Process a file line by line, exit if the line is found: | ||
+ | |||
+ | <code bash> | ||
+ | while read line; do | ||
+ | echo $line | grep test && break; | ||
+ | done < file.txt | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Read and Testing return status ===== | ||
+ | |||
+ | <code bash> | ||
+ | while read line; do let c++; echo $line | grep test >/ | ||
+ | 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.1611678968.txt.gz · Last modified: 2021/01/26 16:36 by peter