SlideShare uma empresa Scribd logo
1 de 15
grep
A powerful text search utility

1

Presented By
Nirajan Pant
MTech IT
Kathmandu University
2/16/2014
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

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

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
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

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

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
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

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

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

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

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

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

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
15

Any Questions
Thank you !!!
?
2/16/2014

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

SHELL PROGRAMMING
SHELL PROGRAMMINGSHELL PROGRAMMING
SHELL PROGRAMMING
 
Q2.12: Debugging with GDB
Q2.12: Debugging with GDBQ2.12: Debugging with GDB
Q2.12: Debugging with GDB
 
Linux systems - Linux Commands and Shell Scripting
Linux systems - Linux Commands and Shell ScriptingLinux systems - Linux Commands and Shell Scripting
Linux systems - Linux Commands and Shell Scripting
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Basic commands of linux
Basic commands of linuxBasic commands of linux
Basic commands of linux
 
sed(1)
sed(1)sed(1)
sed(1)
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Bash shell scripting
Bash shell scriptingBash shell scripting
Bash shell scripting
 
Linux basic commands with examples
Linux basic commands with examplesLinux basic commands with examples
Linux basic commands with examples
 
Linux file system
Linux file systemLinux file system
Linux file system
 
Shell & Shell Script
Shell & Shell Script Shell & Shell Script
Shell & Shell Script
 
Linux commands
Linux commandsLinux commands
Linux commands
 
Shell and its types in LINUX
Shell and its types in LINUXShell and its types in LINUX
Shell and its types in LINUX
 
Intro to Linux Shell Scripting
Intro to Linux Shell ScriptingIntro to Linux Shell Scripting
Intro to Linux Shell Scripting
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
Basic unix commands
Basic unix commandsBasic unix commands
Basic unix commands
 
Basic 50 linus command
Basic 50 linus commandBasic 50 linus command
Basic 50 linus command
 
Know the UNIX Commands
Know the UNIX CommandsKnow the UNIX Commands
Know the UNIX Commands
 
Process and Threads in Linux - PPT
Process and Threads in Linux - PPTProcess and Threads in Linux - PPT
Process and Threads in Linux - PPT
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 

Destaque

Regular Expressions grep and egrep
Regular Expressions grep and egrepRegular Expressions grep and egrep
Regular Expressions grep and egrep
Tri Truong
 
Unix command-line tools
Unix command-line toolsUnix command-line tools
Unix command-line tools
Eric Wilson
 
Advance unix(buffer pool)
Advance unix(buffer pool)Advance unix(buffer pool)
Advance unix(buffer pool)
Sambo Das
 
Some basic unix commands
Some basic unix commandsSome basic unix commands
Some basic unix commands
aaj_sarkar06
 
Linux 101-hacks
Linux 101-hacksLinux 101-hacks
Linux 101-hacks
shekarkcb
 
Presentation1 linux os
Presentation1 linux osPresentation1 linux os
Presentation1 linux os
joycoronado
 

Destaque (20)

Regular Expressions grep and egrep
Regular Expressions grep and egrepRegular Expressions grep and egrep
Regular Expressions grep and egrep
 
Grep
GrepGrep
Grep
 
Learning Grep
Learning GrepLearning Grep
Learning Grep
 
Linux intro 3 grep + Unix piping
Linux intro 3 grep + Unix pipingLinux intro 3 grep + Unix piping
Linux intro 3 grep + Unix piping
 
Unix command-line tools
Unix command-line toolsUnix command-line tools
Unix command-line tools
 
Advance unix(buffer pool)
Advance unix(buffer pool)Advance unix(buffer pool)
Advance unix(buffer pool)
 
Unix
UnixUnix
Unix
 
7th sem it_CSVTU
7th sem it_CSVTU7th sem it_CSVTU
7th sem it_CSVTU
 
Artificial Intelligence Lab File
Artificial Intelligence Lab FileArtificial Intelligence Lab File
Artificial Intelligence Lab File
 
UNIX - Class6 - sed - Detail
UNIX - Class6 - sed - DetailUNIX - Class6 - sed - Detail
UNIX - Class6 - sed - Detail
 
Some basic unix commands
Some basic unix commandsSome basic unix commands
Some basic unix commands
 
Learning sed and awk
Learning sed and awkLearning sed and awk
Learning sed and awk
 
Hill-climbing #2
Hill-climbing #2Hill-climbing #2
Hill-climbing #2
 
Linux 101-hacks
Linux 101-hacksLinux 101-hacks
Linux 101-hacks
 
Hillclimbing search algorthim #introduction
Hillclimbing search algorthim #introductionHillclimbing search algorthim #introduction
Hillclimbing search algorthim #introduction
 
QSpiders - Unix Operating Systems and Commands
QSpiders - Unix Operating Systems  and CommandsQSpiders - Unix Operating Systems  and Commands
QSpiders - Unix Operating Systems and Commands
 
Chapter 2 (final)
Chapter 2 (final)Chapter 2 (final)
Chapter 2 (final)
 
Heuristic Search Techniques {Artificial Intelligence}
Heuristic Search Techniques {Artificial Intelligence}Heuristic Search Techniques {Artificial Intelligence}
Heuristic Search Techniques {Artificial Intelligence}
 
Presentation1 linux os
Presentation1 linux osPresentation1 linux os
Presentation1 linux os
 
Hill climbing
Hill climbingHill climbing
Hill climbing
 

Semelhante a Grep - A powerful search utility

Unit 8 text processing tools
Unit 8 text processing toolsUnit 8 text processing tools
Unit 8 text processing tools
root_fibo
 
15 practical grep command examples in linux
15 practical grep command examples in linux15 practical grep command examples in linux
15 practical grep command examples in linux
Teja Bheemanapally
 
15 practical grep command examples in linux
15 practical grep command examples in linux15 practical grep command examples in linux
15 practical grep command examples in linux
Teja Bheemanapally
 
101 3.7 search text files using regular expressions
101 3.7 search text files using regular expressions101 3.7 search text files using regular expressions
101 3.7 search text files using regular expressions
Acácio Oliveira
 
Hex file and regex cheat sheet
Hex file and regex cheat sheetHex file and regex cheat sheet
Hex file and regex cheat sheet
Martin Cabrera
 
Talk Unix Shell Script
Talk Unix Shell ScriptTalk Unix Shell Script
Talk Unix Shell Script
Dr.Ravi
 
Talk Unix Shell Script 1
Talk Unix Shell Script 1Talk Unix Shell Script 1
Talk Unix Shell Script 1
Dr.Ravi
 
Lecture 18 - Regular Expressions.pdf
Lecture 18 - Regular Expressions.pdfLecture 18 - Regular Expressions.pdf
Lecture 18 - Regular Expressions.pdf
Saravana Kumar
 

Semelhante a Grep - A powerful search utility (20)

Unix
UnixUnix
Unix
 
Unit 8 text processing tools
Unit 8 text processing toolsUnit 8 text processing tools
Unit 8 text processing tools
 
101 3.7 search text files using regular expressions
101 3.7 search text files using regular expressions101 3.7 search text files using regular expressions
101 3.7 search text files using regular expressions
 
101 3.7 search text files using regular expressions
101 3.7 search text files using regular expressions101 3.7 search text files using regular expressions
101 3.7 search text files using regular expressions
 
15 practical grep command examples in linux
15 practical grep command examples in linux15 practical grep command examples in linux
15 practical grep command examples in linux
 
15 practical grep command examples in linux
15 practical grep command examples in linux15 practical grep command examples in linux
15 practical grep command examples in linux
 
101 3.7 search text files using regular expressions
101 3.7 search text files using regular expressions101 3.7 search text files using regular expressions
101 3.7 search text files using regular expressions
 
Cheatsheet: Hex file headers and regex
Cheatsheet: Hex file headers and regexCheatsheet: Hex file headers and regex
Cheatsheet: Hex file headers and regex
 
Hex file and regex cheat sheet
Hex file and regex cheat sheetHex file and regex cheat sheet
Hex file and regex cheat sheet
 
3.7 search text files using regular expressions
3.7 search text files using regular expressions3.7 search text files using regular expressions
3.7 search text files using regular expressions
 
Tutorial on Regular Expression in Perl (perldoc Perlretut)
Tutorial on Regular Expression in Perl (perldoc Perlretut)Tutorial on Regular Expression in Perl (perldoc Perlretut)
Tutorial on Regular Expression in Perl (perldoc Perlretut)
 
Course 102: Lecture 13: Regular Expressions
Course 102: Lecture 13: Regular Expressions Course 102: Lecture 13: Regular Expressions
Course 102: Lecture 13: Regular Expressions
 
Perl Presentation
Perl PresentationPerl Presentation
Perl Presentation
 
Talk Unix Shell Script
Talk Unix Shell ScriptTalk Unix Shell Script
Talk Unix Shell Script
 
Spsl II unit
Spsl   II unitSpsl   II unit
Spsl II unit
 
Talk Unix Shell Script 1
Talk Unix Shell Script 1Talk Unix Shell Script 1
Talk Unix Shell Script 1
 
grep.1.pdf
grep.1.pdfgrep.1.pdf
grep.1.pdf
 
grep.1.pdf
grep.1.pdfgrep.1.pdf
grep.1.pdf
 
Don't Fear the Regex WordCamp DC 2017
Don't Fear the Regex WordCamp DC 2017Don't Fear the Regex WordCamp DC 2017
Don't Fear the Regex WordCamp DC 2017
 
Lecture 18 - Regular Expressions.pdf
Lecture 18 - Regular Expressions.pdfLecture 18 - Regular Expressions.pdf
Lecture 18 - Regular Expressions.pdf
 

Último

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Krashi Coaching
 

Último (20)

General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 

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
  • 15. 15 Any Questions Thank you !!! ? 2/16/2014