MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
Grep - A powerful search utility
1. grep
A powerful text search utility
1
Presented By
Nirajan Pant
MTech IT
Kathmandu University
2/16/2014
2. 2
What is grep?
A text manipulation program
Used to find pattern in files or text
global regular expression print (: g/RE/p) /
general regular expression parser (grep)
Other text manipulation commands – cut,
tr, awk, sed
grep family - grep, egrep, and fgrep
Type man grep to find list of options
2/16/2014
3. 3
The grep command syntax
General syntax of grep command
grep [-options] pattern [filename]
Examples:
$ grep pattern filename
$ grep pattern file1 file2
$ grep -i desktop /etc/services
$ grep [yf] /etc/group
$ grep –vi tcp /etc/services
$ ip addr show | grep inet
2/16/2014
4. 4
grep and exit status
If the pattern is found, grep returns an exit
status of 0, indicating success
if grep cannot find the pattern, it returns 1
as its exit status
if the file cannot be found, grep returns
an exit status of 2
Note: Other UNIX utilities such as sed and awk do not use the exit
status to indicate the success or failure of locating a pattern; they
report failure only if there is a syntax error in a command
2/16/2014
5. options
5
Option
Description
-b
Display the block number at the beginning of each line.
-c
Display the number of matched lines.
-h
Display the matched lines, but do not display the
filenames.
-i
Ignore case sensitivity.
-l
Display the filenames, but do not display the matched
lines.
-n
Display the matched lines and their line numbers.
-s
Silent mode.
-v
Display all lines that do NOT match.
-w
Match whole word.
2/16/2014
6. 6
Examples: grep options
$ grep -i desktop /etc/services
$ grep –vi tcp /etc/services
$ grep -v apple fruitlist.txt
$ grep -l ’main’ *.c
lists the names of all C files in the current directory whose
contents mention „main‟.
$ grep -r ’hello’ /home/gigi
searches for „hello‟ in all files under the „/home/gigi‟
directory
$ grep –w ’north’ datafile
Only the line containing the word north is printed, not
northwest
$ grep -c "Error" logfile.txt
2/16/2014
7. 7
Regular expressions and grep
Syntax:
grep "REGEX" filename
Supports three different versions of regular
expression syntax: “basic” (BRE), “extended” (ERE)
and “perl” (PRCE)
bracket expression: [character_list ] matches any
single character in the list e.g. [01234], [A-D]
Example: grep ‟[A-Z][A-Z] [A-Z]‟ datafile
Character classes: predefined names of
characters lists within bracket expressions e.g.
[:alnum:], [:alpha:]
2/16/2014
8. repetition operators:
8
.
The period „.‟ matches any single character
?
The preceding item is optional and matched at most
once.
*
The preceding item will be matched zero or more
times.
+
The preceding item will be matched one or more times.
{n}
The preceding item is matched exactly n times.
{n,}
The preceding item is matched n or more times.
{,m}
The preceding item is matched at most m times. This is
a GNU extension
{n,m}
The preceding item is matched at least n times, but not
more than m times.
2/16/2014
9. 9
Examples: repetition operators
'l..e' Matches lines containing an l, followed by
two characters, followed by an e
' *love' Matches lines with zero or more spaces,
of the preceding characters followed by the
pattern love [here, preceding character is space]
'o{5}„ Matches if line has 5 o‟s
'o{5,}„ at least 5 o‟s
'o{5,10}„ between 5 and 10 o‟s
$ grep ’5..’ datafile
Prints a line containing the number 5, followed
by a literal period and any single character
2/16/2014
10. 10
The Backslash Characters
The ‘’ character, when followed by certain ordinary
characters, takes a special meaning:
‘b’ Match the empty string at the edge of a word.
‘B’ Match the empty string provided it‟s not at the
edge of a word.
‘<’ Match the empty string at the beginning of word.
‘>’ Match the empty string at the end of word.
‘w’ Match word constituent, it is a synonym for
„[_[:alnum:]]‟.
‘W’ Match non-word constituent, it is a synonym for
„[^_[:alnum:]]‟.
‘s’ Match whitespace, it is a synonym for „[[:space:]]‟.
‘S’ Match non-whitespace, it is a synonym for
„[^[:space:]]‟
2/16/2014
11. 11
More with grep
Anchoring: caret ^ and the dollar sign $ matches
beginning and end of a line respectively
Alteration: alternate expressions may be joined
by the infix operator |
Concatenation: regular expressions may be
concatenated
Precedence: whole expression may be enclosed
in parentheses to override the precedence rules
and form a subexpression
Basic vs Extended Regular Expressions: use the
backslashed versions ?, +, {, |, (, and )
instead of ?, +, {, |, (, and )
Environment Variables: affects behavior of grep
e.g. LC_ALL, LC_foo, and LANG
2/16/2014
12. 12
Examples: special characters
grep '<c...h>' /usr/share/dict/words
list all five-character English dictionary words
starting with "c" and ending in "h"
grep ‟<north‟ datafile
grep ‟<north>‟ datafile
grep Exception logfile.txt | grep -v
ERROR
grep ’^n’ file Prints all lines beginning with an n
grep ’4$’ myfile Prints all lines ending with a 4
grep „bratb‟ datafile matches the separate
word ‘rat’
grep „BratB‟ datafile matches „crate‟ but not
‘furry rat’
2/16/2014
13. 13
grep with Shell Pipes
Instead of taking its input from a file, grep often gets its input from a pipe.
mpiuser@cp-master:~$ ls -l /home | grep '^d‘
drwx------ 2 root
root
drwxr-xr-x 27 mpiuser
drwxr-xr-x 25 parlab
16384 Jan 28 13:13 lost+found
mpiuser
parlab
4096 Feb 11 11:18 mpiuser
4096 Feb 4 11:08 parlab
drwxr-xr-x 27 parlab-user parcompute 4096 Feb 2 15:51 parlab-user
mpiuser@cp-master:~$ cat /proc/cpuinfo | grep -i model
model
: 23
model name
: Intel(R) Core(TM)2 Duo CPU
model
: 23
model name
: Intel(R) Core(TM)2 Duo CPU
E7400 @ 2.80GHz
E7400 @ 2.80GHz
2/16/2014
14. 14
references
http://www.computerhope.com/unix/ugrep.htm
Christopher Negus and Christine Bresnahan. 2012. Linux
Bible (8th ed.). Wiley Publishing, p.128-129, p.157
Alain Magloire et al. 1 January 2014. GNU Grep: Print lines
matching a pattern (version 2.16)
http://www.techonthenet.com/unix/basic/grep.php
http://www.thegeekstuff.com/2009/03/15-practical-unixgrep-command-examples/
http://www.cs.gsu.edu/~cscyip/csc3320/grep.pdf
2/16/2014