Grep Tutorials

Search for a pattern in current directory:
grep -w : match exact word
grep -i : ignore case
grep -v : Dont mach this
grep -l : Only show filename that matches the pattern
grep -n : Show the line number where the match is found.

eg:
grep ‘[A-Z]’ files : Lines containing a caps
grep ‘[0-9]’ files : Lines containing a number
grep ‘[A-Z]…[0-9]’ files : Lines containing five character pattern start with cap and end with a digit.
grep ‘.pattern$’ files : lines ending with .pattern

grep pattern *

grep ‘*’ filename : Now the * means a special character an asterisk.

grep ‘[tT]he’ filename

Check For a pattern at the begining of line: (^)
grep "^startingstring" filename
Check For a pattern at the end of line: ($)
grep "endpattern$" filename
Count / Search Number of Empty Lines : (^$)

grep -c “^$” filename

Match single character : (.)

grep -c “str.g” filename

Escape special character: ()

grep “24.249.234.1” /var/log/secure

Matching a character class [ ]:

grep “[0-9]+string” filename

Negating character class [1]:

grep -i “[2]”  filename

[ caret ^ on the front means start of line and inside bracket means except those ]

Count the number of characters:

x=”string”
grep -o “s” <<<“$x” | wc -l

or

y=”${x//[^s|S]}”
echo “${#y}”

Searching for a string in a file:

grep “string” filename

Checking for string in multiple files:

grep “string”  filename*

Checking for a string recursively in a directory:

grep -r “string” directory

Search for string ignoring the case:

grep -i “string” filename

Searching with Regular Expressions:

grep “string.*string2” filename

Other Operators for Regex:
? : Anything before this character is optional and matched at most once.

  • : Anything after this will be matched zero or more times.
  • : After this character will be matched one or more times
    {n}: After this character will be matched exactly n times.
    {n,}:After this will be matched n or more times.
    {,m}:After this will be matched at most m times.
    {n,m}:After this will be matched at least n times and at most m times.

Search full words, not sub-strings:

grep -w “is” filename

This will search for is as a word, doesnot match this.

Show N lines after search string:
grep -A “string” filename

Show N lines before search string:

grep -B “string” filename

Show N lines around the search string:

grep -C “search string” filename

Highlight the searched string:

export GREP_OPTIONS=’–color=auto’ GREP_COLOR=’100;8′

now any grep will highlight the searched string.

Reverse match :

grep -v “dontmatchthis” filename

Display lines which dont match the search pattern:

grep -v -e “pattern” -e “pattern”

Count the number of total matches :

grep -c “string” filename

Search the filenames that match the given pattern or string:

grep -l filename

Show only matched string:

grep -o “is.*line” filename

Show the position of the match in line :

grep -o -b “string” filename

Show line number with output:

grep -n “string” filename

Grep and display lines around match
This will display lines around the matching word.

grep -A 10 -B 10 "word" filename.txt


  1. ↩︎

  2. ^anyletterinhere ↩︎