Together with egrep and fgrep, grep print lines matching a pattern from inputs.

egrep and fgrep are two variant programs of grep. 

egrep is the same as grep -E. 
fgrep is the same as grep -F

Example 1: simple string search

$ grep "NTP" /var/log/boot.log 
    Starting NTP client/server...

or print string with color
$grep --color "NTP" /var/log/boot.log

or from a input file
grep aaa -

Note: '-' is the keyboard input

Example 2: Invert the matching

$ grep -v bash /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
...

In the case above,
-v, --invert-match
Invert the sense of matching, to select non-matching lines.
  (-v is specified by POSIX.)

Example 3: Prefix each line of output with the base line number of input file


$ grep -n bnx2 /var/log/dmesg
1128:bnx2: Broadcom NetXtreme II Gigabit Ethernet Driver bnx2 v2.2.3 (June 27, 2012)
1129:bnx2 0000:03:00.0: PCI INT A -> GSI 16 (level, low) -> IRQ 16
1130:bnx2 0000:03:00.0: firmware: requesting bnx2/bnx2-mips-06-6.2.3.fw

Where
-n, --line-number
Prefix each line of output with the 1-based line number within its input file.
(-n is specified by POSIX.)

Example 4: print a count of matching lines for each input file

$ grep -c bash /etc/passwd
17

Where
-c, --count
Suppress normal output; instead print a count of matching lines for each input file. 
With the -v, --invert-match  option  (see below), count non-matching lines. 
(-c is specified by POSIX.)

Example 5: ignore case distinctions

$grep -i pci /var/log/dmsg
...
[drm] PCI GART of 512M enabled (table at 0x0000000037880000).
input: Avocent IBM 73P5832 as /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1:1.0/input/input2
input: Avocent IBM 73P5832 as /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1:1.1/input/input3
  ...

Where
-i, --ignore-case
    Ignore case distinctions in both the PATTERN and the input files. 
(-i is specified by POSIX.)

Example 6: Select only those lines containing matches that form whole words

$ grep -w bnx /var/log/dmesg
$ grep -w bnx2 /var/log/dmesg
bnx2: Broadcom NetXtreme II Gigabit Ethernet Driver bnx2 v2.2.3 (June 27, 2012)
bnx2 0000:03:00.0: PCI INT A -> GSI 16 (level, low) -> IRQ 16
bnx2 0000:03:00.0: firmware: requesting bnx2/bnx2-mips-06-6.2.3.fw
bnx2 0000:03:00.0: firmware: requesting bnx2/bnx2-rv2p-06-6.0.15.fw

Where
-w, --word-regexp
     Select only those lines containing matches that form whole words. 
The test is that the matching substring must  either  be  at the  beginning  of  the  line,
or preceded by a non-word constituent character. 
Similarly, it must be either at the end of the line or followed by a non-word constituent
character.  Word-constituent characters are letters, digits, and the underscore.

Example 7: Select only those matches that exactly match the whole line

$ grep -x bnx2 /var/log/dmesg
$ grep -x "bnx2 0000:03:00.0: PCI INT A -> GSI 16 (level, low) -> IRQ 16" /var/log/dmesg
bnx2 0000:03:00.0: PCI INT A -> GSI 16 (level, low) -> IRQ 16

Where
-x, --line-regexp

Example 8:  Suppress normal output

$ grep -l bnx /var/log/dmesg /var/log/messages
/var/log/dmesg
$ grep -L bnx /var/log/dmesg /var/log/messages
/var/log/messages

Where
-L, --files-without-match
    Suppress normal output; instead print the name of each input file from which no output
would normally have been  printed.   The scanning will stop on the first match.
-l, --files-with-matches
Suppress  normal  output;  instead  print  the name of each input file from which output
would normally have been printed.  The scanning will stop on the first match. 
(-l is specified by POSIX.)

Example 9: Recursive search

$grep -r mtx tape | more
tape/mtx-1.2.18rel/mam2debug2.c:#include "mtx.h"
tape/mtx-1.2.18rel/mam2debug2.c:#include "mtxl.h"
...

Example 10, print 2 lines before and after matched line

$ grep --color -A 2 -B 2  "command line" /var/log/dmesg
Built 1 zonelists in Zone order, mobility grouping on.  Total pages: 4133096
Policy zone: Normal
Kernel command line: ro root=UUID=8e37d096-4494-4a31-80df-e74b6a8c26e3 rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD quiet SYSFONT=latarcyrheb-sun16  crashkernel=130M@0M  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM
PID hash table entries: 4096 (order: 3, 32768 bytes)
Checking aperture...

$ grep --color -C 2 "command line" /var/log/dmesg
Built 1 zonelists in Zone order, mobility grouping on.  Total pages: 4133096
Policy zone: Normal
Kernel command line: ro root=UUID=8e37d096-4494-4a31-80df-e74b6a8c26e3 rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD quiet SYSFONT=latarcyrheb-sun16  crashkernel=130M@0M  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM
PID hash table entries: 4096 (order: 3, 32768 bytes)
Checking aperture...
Match regular expression in files

Example 11:  search lines start with 'string'

$grep ^root /etc/passwd
root:x:0:0:root:/root:/bin/bash

Example 12, remove empty lines

$grep -v ^$ README.rst
===========
PgFincore
===========
--------------------------------------------------------------
A set of functions to manage pages in memory from PostgreSQL
--------------------------------------------------------------
A set of functions to handle low-level management of relations using mincore to
explore cache memory.

 Example 13 '-E' option, search multiple words

grep -w -E 'bnx|pci' /var/log/dmsg
...
pci 0000:00:1f.0: rerouting interrupts for [8086:2670]
pci 0000:01:06.0: Boot video device
bnx2: Broadcom NetXtreme II Gigabit Ethernet Driver bnx2 v2.2.3 (June 27, 2012)
bnx2 0000:03:00.0: PCI INT A -> GSI 16 (level, low) -> IRQ 16

Example 14 Obtain patterns from a file

grep -f /home/pattern.lst /var/log/dmsg

Note: one pattern per line

Example 15 regular expression pattern

$ grep "0000\:00\:0[1-9]" /var/log/dmesg
pci 0000:00:02.0: PME# supported from D0 D3hot D3cold
pci 0000:00:02.0: PME# disabled
pci 0000:00:03.0: PME# supported from D0 D3hot D3cold
pci 0000:00:03.0: PME# disabled
pci 0000:00:04.0: PME# supported from D0 D3hot D3cold
pci 0000:00:04.0: PME# disabled

Example 16 matching neither of words

$ grep -w -e "pcieport" -e "bnx2" /var/log/dmesg
pcieport 0000:00:1c.0: setting latency timer to 64
pcieport 0000:00:1c.0: irq 24 for MSI/MSI-X
pcieport 0000:00:1c.1: setting latency timer to 64
pcieport 0000:00:1c.1: irq 25 for MSI/MSI-X
bnx2: Broadcom NetXtreme II Gigabit Ethernet Driver bnx2 v2.2.3 (June 27, 2012)
bnx2 0000:03:00.0: PCI INT A -> GSI 16 (level, low) -> IRQ 16
bnx2 0000:03:00.0: firmware: requesting bnx2/bnx2-mips-06-6.2.3.fw
bnx2 0000:03:00.0: firmware: requesting bnx2/bnx2-rv2p-06-6.0.15.fw
bnx2 0000:03:00.0: eth0: Broadcom NetXtreme II BCM5708 1000Base-T (B2) PCI-X 64-bit 133MHz found at mem ce000000, IRQ 16, node addr 00:1a:64:63:3f:00
bnx2 0000:06:00.0: PCI INT A -> GSI 17 (level, low) -> IRQ 17