Thursday 1 August 2019

Linux - grep egrep fgrep rgrep

Name

grep, egrep, fgrep, rgrep [man page] - print lines matching a pattern

Synopsis

grep [options] PATTERN [FILE...]
grep [options] [-e PATTERN | -f FILE] [FILE...] 

Examples

grep

To begin this linux grep command line tutorial first we need to create a sample files to play with, run commands below:
echo -e "Redhatlinux\nDebianLInux4.0\nPCLinuxOS\n 
OpenSUSE\nLinUxFEDORA\n\nMandriva" > file.grep_1
echo -e "MintLinux\nLinuxGYM1.0.3\nUbuntu\n 
Kubuntu\nSlaxLiNuX\n\nKnoppix" > file.grep_2 
As you already assume grep can be used to search string within a file. Lets search for word "linux":
grep linux * 
grep can be used to search string within a file
As everything else in Linux grep is also case sensitive, to ignore case we need to use grep with combination of -i option:
grep -i linux * 
grep ignore case sensitivity
grep option -h allows us to suppress to display file name and with -n option grep numbers lines within a file. --colour=auto highlights match:
grep -inh --colour=auto linux * 
grep suppress display file name and display line numbers
If we do not want grep to output lines which does NOT contain lines with keyword linux we use -v ( invert ) option:
grep -iv linux * 
grep with invert option
With -c grep can count string occurrences within files, so here the grep will print number of how many times keyword linux does NOT appear within both files:
grep -icv linux * 
use grep to count pattern occurrences within files
If we are looking for the name of the file which contains keyword "Ubuntu" we would use grep's -l option:
grep -l Ubuntu * 
use grep to search for pattern in the file names
Grep with -x option will print exact occurrences only.
grep -ixc linux * 
Grep with -x option will print exact occurrences only
Grep can even use a file which contain match pattern:
echo "LinUx" > grep_pattern.txt
grep -f grep_pattern.txt * 
grep can use a pattern template from a file
System administrators will definitely appreciate following grep options when search log files. -B3 ( display 3 lines before match ) and -A3 ( display 3 lines after ) match. To make it even more readable --colour=auto can be used:
grep -B3 -A3 --colour=auto "command" /var/log/dmesg 
Display lines before and after pattern match

Grep & regex

Grep and Regular Expressions ( regex ). This topic can definitely cover whole book but it would be shame to not show at least couple examples for grep and regular expressions. For example to make grep return only lines which contains digits, we use a command:
grep [0-9] * 
use grep to search lines which contain digits
To count all empty lines within a file using grep we use command:
grep -ch ^$ file.grep_2 
Grep can count empty lines within a file
Let's see what line starts with "L" and ends with number ( ^ -> match beginning of the line, $ -> match end of the line):
grep ^L.*[0-9]$ * 
use grep to match a pattern at the beginning of the line
To make grep match only lines where "b" is a third character in the word we can use linux command:
grep ..b  * 
match third character in the string

egrep

egrep is extended version of grep or egrep is equal to grep -E. Egrep supports more regular expression patterns. Lets search for lines which contains exactly two consecutive p characters:
egrep p{2} *
OR
grep pp *
OR
grep -E p{2} * 
Match line with two consecutive p characters
lets get an output of egrep command with all lines which ends with "S" OR "A" :
egrep "S$|A$" * 
grep or function to match pattern at the end of the line

fgrep

fgrep is a faster version of grep which does not support regular expressions and therefore is considered to be faster. fgrep is equal to grep -F. Simple proof that fgrep does not interpret regular expressions (regex):
fgrep linux$ *
egrep linux$ *
grep linux$ * 
fgrep does not interpret regular expressions

rgrep

rgrep is a recursive version of grep. Recursive in this case means that rgrep can recursively descend through directories as it greps for the specified pattern. rgrep is similar to grep -r.
mkdir -p dir/dir1
echo "Linux_rgrep" > dir/dir1/rgrep.txt
grep rgrep *
rgrep rgrep *
grep -r rgrep * 
rgrep is recursive version of grep

0 comments:

Post a Comment