grep Command

The grep command is used to search text. It searches the given file for lines containing a match to the given strings or words. It is one of the most useful commands on Linux and Unix-like system.

Let us see how to use grep on a Linux or Unix like system.
$man grep
NAME
grep, egrep, fgrep, rgrep - print lines matching a pattern
SYNOPSIS
grep [OPTIONS] PATTERN [FILE...]
grep [OPTIONS] -e PATTERN ... [FILE...]
grep [OPTIONS] -f FILE ... [FILE...]
DESCRIPTION
grep searches for PATTERN in each FILE. A FILE of “-” stands for
standard input. If no FILE is given, recursive searches examine the
working directory, and nonrecursive searches read standard input. By
default, grep prints the matching lines.
In addition, the variant programs egrep, fgrep and rgrep are the same
as grep -E, grep -F, and grep -r, respectively. These variants are
deprecated, but are provided for backward compatibility.

Examples of using ‘grep’

grep foo /file/name
Searches the file /file/name for the word ‘foo’. Each match will be displayed on a separate line.

grep -i “foo” /file/name
The option -i can be helpful Searches /file/name for ‘foo’ ignoring case of the word, ie foo Foo FOO etc.

grep ‘error 123’ /file/name
grep is not limited to searching for just single words or strings. It can also search for sequences of strings, including phrases. This is achieved by enclosing the sequence of strings that forms the pattern in quotation marks (either single or double). The example above will search for the phrase ‘error 123’ in the file /file/name.

grep -r “192.168.1.5” /etc/
grep’s search area can be broadened further by using its -r option to search recursively through an entire directory tree (i.e. a directory and all sub-directories within it). The example above searches all files in the /etc/ directory and all of its sub-directories (including their sub-directories) for the string ‘192.168.1.5’

grep -w “foo” /file/name
When you search for foo, grep will match fooboo, foo123, etc. You can force grep to select only those lines containing matches that form whole words by using the -w option.

egrep -w ‘word1|word2’ /file/name
Searches for, and displays 2 different words in /file/name

grep -c test /file/name
The -c option causes grep to only report the number of times that the pattern has been matched for each file, and to not display the actual lines. The example above would show the total number of times that the string “test” appears in the file /file/name.

grep –context=6 error /file/name.txt
Sometimes we are not just interested in the matching line but also on the lines around matching lines, this is where the grep –context option is useful. It can be particularly helpful to see what happens before or after any Error or Exception. In the example above the –context option is used to print the 6 lines before and after a matching line with the word “error” in /file/name.txt

Above are some simple examples to get you started using the grep command. The search functionality of grep can be refined further through the use of ‘regular expressions’. This is a pattern matching system that uses strings constructed according to pre-defined rules to find desired patterns in text. Additional information about grep, including its use with regular expressions, can be obtained from its built-in manual page by using the ‘man’ command, i.e., man grep

Post a Comment

Post a Comment (0)

Previous Post Next Post