Sunday, 17 April 2016

How to search something in files using grep ?


GREP (Global regular expression print) is a command line search utility or tool  to filter the input given to it.

grep options searchItem fileName
OR
command | grep options searchItem

Examples
  • Searching a word in file
    grep 'shaan' employee.txt
     
  • Reverse your search; Search all words other than "shaan"
    grep -v ‘shaan’ employee.txt
  • Search a word using case insensitive (ignore case)
    grep -i 'shaan' employee.txt
  • Count a word occurrence
    find -c ‘shaan’ employee.txt
  • Find word along with line numbers
    grep -n 'shaan' employee.txt
  • Find exact word
    grep -x 'shaan' employee.txt
  • Searching a word in files under the hierarchy of a directory recursively
    grep -r 'shaan' /home/myuser/
  • Search for a word and just list the files which contains it within a directory
    grep -l ‘shaan’ /home/myuser/

Basic regular expressions used in grep
  ^  Match beginning of the line
  $  Match End of the line
  *  Match 0 or more occurrence of previous character
  .  Match Any single character
  [ ]  Match Range of characters, just single occurrence
  [a-z]   Match small letters
  [A-Z]   Match cap letters
  [0-9]   Match numerical
  [^]   Match Negate a sequence
  \   Match Escape character


Example
  • Find all the lines which start with “Mr”
    grep ‘^Mr’ filename

  • Find all the lines which ends with ‘sh’
    grep ‘sh$’ filename

  • Display all the lines in a file expect empty lines
    grep –v ‘^$’ filename

    Note : -v option is used to negate the search term, here ^$ indicates empty line, so our grep –v is filtering empty lines in the output.
  • Search for words which are bash, baash, bsh, baaash, baaaaash, ...
    grep ‘ba*s’ filename

    This will search for words which has a between b and s zero or more times.
  • Search for all words which starts with b and h
    grep ‘b.*h’ filename
     
  • Search for a word which is having three letters in it and starts with x and ends with m
    grep ‘x[a-z]m’ filename

    This search will return all the 3 letter words which start with x and ends with m.
     
  • Search words do not contain ‘ac’ in a file grep ‘[^ac]’ filename
  • Search for a ‘[‘ in a file
    Note : The “[“is a special character, you cannot search with normal grep we have to use escape character (\) in order to negate it.

    So use ‘\[‘ to search for [. This is applicable for all the special characters mention above.

    grep ‘\[’ filename

Extended regular expressions (grep -E option)
  +  Match one or more occurrences of previous character
  |  Match either character
  ?  Match 0 or 1 occurrence of previous character
  ()  Match a group of characters
  {number}   Match number of occurrence of a character
  {1, 3}  Match a character which is 1 to 3 times repetition
  {5, }   Match a repeated character which is repeated 5 or more times

Note : In order to use this extended regular expressions we have to use –E option give grep the capability to understand Extended regular expressions.
egrep is nothing but grep –E, so try to avoid it if grep itself can do the work for you.


Examples
  • Search for a words which contains one or more occurrence of ‘b’ between a and c
    grep –E ‘ab+c’ filename
  • Search for a word which contains zero or one occurrence of b between a and c
    grep –E ‘ab?c’ filename
  • Search for a word which contains either a or b, a and b between d, e characters
    grep –E ‘da|be’ filename
  • Search for a word which contains either a or b, but not both a and b between d, e characters
    grep –E ‘d(a|b)e’ filename
     
  • Search for a word which contains only 2 ‘b’ between a and c character
    grep –E ‘ab{2}c’ filename
  • Search for a word which contains 3 to 4 ‘b’ between a and c character
    grep –E ‘a
    b{2,4}c’ filename 
  • Search for a word which contains 3 or more ‘b’ between a and c character
    grep –E ‘ab{3, }c’ filename

No comments:

Post a Comment

Note: only a member of this blog may post a comment.