Thursday, March 14, 2013

grep examples: Search multiple patterns in a file using grep


grep -E "pattern1|pattern2" filename

or

egrep '(pattern1|pattern2)' filename

----------------------------------------------------------------------------------------------------

To number the output as lines.

grep -n root /etc/passwd

1:root:x:0:0:root:/root:/bin/bash
12:operator:x:11:0:operator:/root:/sbin/nologin

----------------------------------------------------------------------------------------------------

Count the occurence of word fals in /etc/passwd

grep -c false /etc/passwd
7

----------------------------------------------------------------------------------------------------

From the previous example, we now exclusively want to display lines starting with the string "root":

cathy ~> grep ^root /etc/passwd
root:x:0:0:root:/root:/bin/bash
----------------------------------------------------------------------------------------------------
If we want to see which accounts have no shell assigned whatsoever, we search for lines ending in ":":

cathy ~> grep :$ /etc/passwd
news:x:9:13:news:/var/spool/news:
----------------------------------------------------------------------------------------------------
To check that PATH is exported in ~/.bashrc, first select "export" lines and then search for lines starting with the string "PATH", so as not to display MANPATH and other possible paths:

cathy ~> grep export ~/.bashrc | grep '\ matches the end of a word.

If you want to find a string that is a separate word (enclosed by spaces), it is better use the -w, as in this example where we are displaying information for the root partition:

cathy ~> grep -w / /etc/fstab
LABEL=/ / ext3 defaults 1 1

If this option is not used, all the lines from the file system table will be displayed.
----------------------------------------------------------------------------------------------------


cathy ~> grep [yf] /etc/group
sys:x:3:root,bin,adm
tty:x:5:
mail:x:12:mail,postfix
ftp:x:50:
nobody:x:99:
floppy:x:19:
xfs:x:43:
nfsnobody:x:65534:
postfix:x:89:

In the example, all the lines containing either a "y" or "f" character are displayed.

----------------------------------------------------------------------------------------------------

Use the "." for a single character match. If you want to get a list of all five-character English dictionary words starting with "c" and ending in "h" (handy for solving crosswords):

cathy ~> grep '\' /usr/share/dict/words
catch
clash
cloth
coach
couch
cough
crash
crush
----------------------------------------------------------------------------------------------------
If you want to display lines containing the literal dot character, use the -F option to grep.

For matching multiple characters, use the asterisk. This example selects all words starting with "c" and ending in "h" from the system's dictionary:

cathy ~> grep '\' /usr/share/dict/words
caliph
cash
catch
cheesecloth
cheetah
--output omitted--
----------------------------------------------------------------------------------------------------
If you want to find the literal asterisk character in a file or output, use single quotes. Cathy in the example below first tries finding the asterisk character in /etc/profile without using quotes, which does not return any lines. Using quotes, output is generated:

cathy ~> grep * /etc/profile

cathy ~> grep '*' /etc/profile
for i in /etc/profile.d/*.sh ; do

No comments:

Post a Comment