Yuhang He's Blog

Some birds are not meant to be caged, their feathers are just too bright.

Return to the details of grep

grep is often used together with sed, awk to process file. It is often used to search specified pattern among file pools. grep: global search regular expression. Its intuitive usage is to search patterns with regular expression and further print searched patterns out. It always works as:

1
2
3
4
5
6
7
grep [-acinv] [--color=auto] 'pattern' file_name
-a: process the binary file as if it were text file;
-c: instead of print out the whole lines, it prints the counts of searched lines for a given file;
-i: ignore the capital and small case of a letter.
-n: print out the line number of each searched file lines;
-v: search for the pattern that not equals to 'pattern';
--color=auto: print the pattern in highlighted color.

If you want to search multiple patterns at the same time, you may turn to |, egrep or grep -E. For example:

1
2
3
4
grep 'a' file_name | grep 'b'
egrep 'a|b' file_name
grep -E 'a|b' file_name
grep 'a\|b' file_name

grep searches patterns in various files, sometimes we have to recursively search the pattern from files at various directories. Or we just need to return the file names containing the pattern. We can use -l, -r to achieve this:

1
2
3
grep 'pattern' * //search all files at current directory
grep -r 'pattern' * //search all files at either current directories or subdirectories
grep -l -r 'pattern'  *//only returns the file names that contain pattern

regular expression is often used in grep. Similar to other relevant regular expression awk, sed, grep often uses regular expression to approximately search pattern. Some common regular expressions include: . means must include one character, * means repeat the prior character 0 or N times. For example:

1
2
3
4
grep -n '[0-9][0-9]*' file_name // return the digits.
grep -n 'g.*g' file_name // search the pattern starting and ending with g.
grep -n '^$' file_name. // search the blank line.
grep -n '\.$' file_name. //search the line ending with .

To further specify the number of character we want to search, we should turn to \{\}. For example:

1
2
3
grep -n 'go\{2,5\}gle' file_name //should search 2-5 o
grep -n 'go\{2\}gle' file_name //should search only two o
grep -n 'go\{2,\}gle' file_name //should search at least two o

Another way to search for lines containing pattern with more than one occurrence is to use +

1
2
3
egrep '(o)+' file_name
grep -E '(o)+' file_name
grep '(o)\+' file_name