grep command
usage of grep commands
Last updated
usage of grep commands
Last updated
`grep - global regular expression print`
Searches for particular string/keyword from a file and print lines matching a pattern
Checks line by line and print lines matching a pattern
Sample file
grep [pattern] [filename]
grep -i “keyword” filename
→ Ignore case of the pattern
grep -v “keyword” filename
→ Show all records except the pattern
grep -c “keyword” file
→ count number of lines in which word is present
grep -w “keyword” file
→ exact match of a word
grep -n “keyword” file
→ also prints the line number
grep “keyword” file1 file2 file3 ……
→ search in multiple files
grep -e “keyword” -e “keyword” -e “keyword” file
→ search for multiple keywords in a file
grep -e “keyword” -e “keyword” -e “keyword” file1 file2 …..
→ similarly to search for multiple keywords in multiple files
grep -l “keyword” file1 file2 file3
…..
→ only get file name in which the word is present
grep “^keyword” file
→ print matching line which starts from matching keyword
grep “keyword$” file
→ print matching line that ends with given word
grep -R “keyword” file
→ find in every file in a directory recursively
egrep “key1|key2|key3” filename
→ search multiple keywords
grep -q “keyword” filename
- search but don't print on terminal
Check the status by echo $? → exit status
grep -s “keyword” filename
→ suppress errors
Use to search files → ls | grep rc