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 *
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 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 -iv linux *
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 *
If we are looking for the name of the file which contains keyword "Ubuntu" we would use grep's -l option:
grep -l Ubuntu *
Grep with -x option will print exact occurrences only.
grep -ixc linux *
Grep can even use a file which contain match pattern:
echo "LinUx" > grep_pattern.txt grep -f grep_pattern.txt *
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
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] *
To count all empty lines within a file using grep we use command:
grep -ch ^$ file.grep_2
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]$ *
To make grep match only lines where "b" is a third character in the word we can use linux command:
grep ..b *
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} *
lets get an output of egrep command with all lines which ends with "S" OR "A" :
egrep "S$|A$" *
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$ *
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 *
0 comments:
Post a Comment