Grep is one of the most widely used and powerful commands on Linux and Unix-like operating systems. The ‘grep’ command is used to search a given file for patterns specified by the user which comes handy for file manipulation operations like an event search in logs files, configuration options in config files etc.
Grep gives the option to search a string against some file contents or a list of outputs from an execution result and gives only the lines containing the search patterns depending on the option flags used. Strings must be enclosed in in quotations to ensure the program correctly recognizes them as a single string if it includes a space or special character in it.
The grep command syntax is as noted below.
Usage: grep [OPTION]... PATTERN [FILE].. Search for PATTERN in each FILE or standard input.
Some practical examples are as shown below for your reference.
The sample file that we use here is named as eurovpstest.txt which include some test contents.
[root@eurovps ~]# cat eurovpstest.txt Managed VPS Custom VPS Bare Metal Servers Private Cloud Managed Clusters Shared Hosting Reseller Hosting Since launching in April of 2004, EuroVPS now manages the hosting of +2,000 clients in +100 countries. We'd love for you to join!
Searching a string
Like in most of the Linux criterias, grep search are also case sensitive
[root@eurovps ~]# grep hosting eurovpstest.txt Since launching in April of 2004, EuroVPS now manages the hosting of +2,000 clients in +100 countries. We'd love for you to join!
Case insensitive search
[root@eurovps ~]# grep -i hosting eurovpstest.txt Shared Hosting Reseller Hosting Since launching in April of 2004, EuroVPS now manages the hosting of +2,000 clients in +100 countries. We'd love for you to join!
Here the search pattern that we used is hosting however grep searched it without case sensitivity and returned strings which contained Hosting
Searching string with multiple words
[root@eurovps ~]# grep "Custom VPS" eurovpstest.txt Custom VPS
Here the search pattern used contains multiple strings and we have to use “ ” to include our search pattern as a single input.
Strict word search
[root@eurovps ~]# grep -w VPS eurovpstest.txt Managed VPS Custom VPS
As you can see the grep search returned only specific strings which contain VPS and it excluded the strings like EuroVPS.
Searching multiple strings
[root@eurovps ~]# grep 'Servers\|Hosting' eurovpstest.txt Bare Metal Servers Shared Hosting Reseller Hosting
Here the grep search returned all the lines containing strings Servers and Hosting.
We can include the case insensitive flag to get all the lines containing the word hosting irrespective of the alphabet case.
[root@eurovps ~]# grep -i 'Servers\|Hosting' eurovpstest.txt Bare Metal Servers Shared Hosting Reseller Hosting Since launching in April of 2004, EuroVPS now manages the hosting of +2,000 clients in +100 countries. We'd love for you to join!
Grep to find number of occurrences of a string
[root@eurovps ~]# grep -c hosting eurovpstest.txt 1
[root@eurovps ~]
# grep -ci hosting eurovpstest.txt 3
Here in the second case, we combined the flags to count the matches and case insensitivity to get total occurance of the string hosting in the file.
Output nearby entries above and below the match.
[root@eurovps ~]# grep --context=2 Private eurovpstest.txt Custom VPS Bare Metal Servers Private Cloud Managed Clusters Shared Hosting
Here grep returned 2 lines above and below the string match (Private). The abbreviated form of context flag is -C which can be used to get the same output string.
[root@eurovps ~]# grep -C 2 Private eurovpstest.txt Custom VPS Bare Metal Servers Private Cloud Managed Clusters Shared Hosting
Similarly -A, –after-context=NUM and -B, –before-context=NUM can be used to get number of lines before and after the string match.
[root@eurovps ~]# grep -A 2 Private eurovpstest.txt Private Cloud Managed Clusters Shared Hosting
[root@eurovps ~]
# grep -B 2 Private eurovpstest.txt Custom VPS Bare Metal Servers Private Cloud
Above mentioned are some sample scenarios which can help you start with the grep command and below mentioned are the full command flags that can be used with grep command.
Regexp selection and interpretation:
-E, --extended-regexp PATTERN is an extended regular expression (ERE) -F, --fixed-strings PATTERN is a set of newline-separated fixed strings -G, --basic-regexp PATTERN is a basic regular expression (BRE) -P, --perl-regexp PATTERN is a Perl regular expression -e, --regexp=PATTERN use PATTERN for matching -f, --file=FILE obtain PATTERN from FILE -i, --ignore-case ignore case distinctions -w, --word-regexp force PATTERN to match only whole words -x, --line-regexp force PATTERN to match only whole lines -z, --null-data a data line ends in 0 byte, not newline Miscellaneous: -s, --no-messages suppress error messages -v, --invert-match select non-matching lines -V, --version display version information and exit --help display this help text and exit Output control: -m, --max-count=NUM stop after NUM matches -b, --byte-offset print the byte offset with output lines -n, --line-number print line number with output lines --line-buffered flush output on every line -H, --with-filename print the file name for each match -h, --no-filename suppress the file name prefix on output --label=LABEL use LABEL as the standard input file name prefix -o, --only-matching show only the part of a line matching PATTERN -q, --quiet, --silent suppress all normal output --binary-files=TYPE assume that binary files are TYPE; TYPE is 'binary', 'text', or 'without-match' -a, --text equivalent to --binary-files=text -I equivalent to --binary-files=without-match -d, --directories=ACTION how to handle directories; ACTION is 'read', 'recurse', or 'skip' -D, --devices=ACTION how to handle devices, FIFOs and sockets; ACTION is 'read' or 'skip' -r, --recursive like --directories=recurse -R, --dereference-recursive likewise, but follow all symlinks --include=FILE_PATTERN search only files that match FILE_PATTERN --exclude=FILE_PATTERN skip files and directories matching FILE_PATTERN --exclude-from=FILE skip files matching any file pattern from FILE --exclude-dir=PATTERN directories that match PATTERN will be skipped. -L, --files-without-match print only names of FILEs containing no match -l, --files-with-matches print only names of FILEs containing matches -c, --count print only a count of matching lines per FILE -T, --initial-tab make tabs line up (if needed) -Z, --null print 0 byte after FILE name Context control: -B, --before-context=NUM print NUM lines of leading context -A, --after-context=NUM print NUM lines of trailing context -C, --context=NUM print NUM lines of output context -NUM same as --context=NUM --group-separator=SEP use SEP as a group separator --no-group-separator use empty string as a group separator --color[=WHEN], --colour[=WHEN] use markers to highlight the matching strings; WHEN is 'always', 'never', or 'auto' -U, --binary do not strip CR characters at EOL (MSDOS/Windows) -u, --unix-byte-offsets report offsets as if CRs were not there (MSDOS/Windows)