Featured image of post Shell grep usage

Shell grep usage

A list of grep options

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
    grep -i # ignore case
    grep -r “words” . ## search words in all files in the current folder, return file name and matched lines; -lr ## output only file names
    grep -r “wordA\|wordB\|wordC”
    grep -lr “wor.*” ./log*  ## search only in files starting with log
    grep '^From: ' /usr/mail/$USER	{list your mail}
    grep '[a-zA-Z]'			{any line with at least one letter}
    grep '[^a-zA-Z0-9]		{anything not a letter or number}
    grep '[0-9]\{3\}-[0-9]\{4\}'	{999-9999, like phone numbers}
    grep '^.$'			{lines with exactly one character}
    grep '"smug"'			{'smug' within double quotes}
    grep '"*smug"*'			{'smug', with or without quotes}
    grep '^\.'			{any line that starts with a Period "."}
    grep '^\.[a-z][a-z]'		{line start with "." and 2 lc letters}
    grep -o # show only matched pattern instead of whole line
    grep -n # add line number
    grep -v # lines not matching 
    grep -B1 -A1 "PATTERN" FILE # search one line before and one line after
comments powered by Disqus
CC-BY-NC 4.0
Built with Hugo Theme Stack