1️⃣grep command

usage of grep commands

`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

  1. grep [pattern] [filename]

  1. grep -i “keyword” filename → Ignore case of the pattern

  1. grep -v “keyword” filename → Show all records except the pattern

  2. grep -c “keyword” file → count number of lines in which word is present

  1. grep -w “keyword” file → exact match of a word

  1. grep -n “keyword” file → also prints the line number

  1. grep “keyword” file1 file2 file3 …… → search in multiple files

  1. grep -e “keyword” -e “keyword” -e “keyword” file → search for multiple keywords in a file

  1. grep -e “keyword” -e “keyword” -e “keyword” file1 file2 ….. → similarly to search for multiple keywords in multiple files

  2. grep -l “keyword” file1 file2 file3 ….. → only get file name in which the word is present

  1. grep “^keyword” file → print matching line which starts from matching keyword

  2. grep “keyword$” file → print matching line that ends with given word

  3. grep -R “keyword” file → find in every file in a directory recursively

  1. egrep “key1|key2|key3” filename → search multiple keywords

  1. grep -q “keyword” filename - search but don't print on terminal

Check the status by echo $? → exit status

  1. grep -s “keyword” filenamesuppress errors

  2. Use to search files → ls | grep rc

Last updated