Loading presentation...

Present Remotely

Send the link below via email or IM


Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.



No description

Arefeen choyon

on 16 April 2016

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Grep

Displaying line number of matches using grep in unix
Some other Important Uses of grep
search for a string in one or more files

grep 'fred' /etc/passwd

#search for lines containing 'fred' in /etc/passwd
grep -i joe users.txt

# find joe, Joe, JOe, JOE, etc.
grep in a pipeline
ps auxwww | grep httpd
#all processes containing 'httpd'

ps auxwww | grep -i java
# all processes containing 'java', ignoring case
grep + find
find . -type f -exec grep -il 'foo' {} \;

#print all filenames of files under current dir containing 'foo', case-insensitive
What is grep?
And the power of Regular Expression
Displaying Context around matching word
Counting number of Matching Words
Finding whole word using grep in Unix Linux
Reverse the meaning
grep -v fred /etc/passwd
#find any line *not* containing 'fred'

grep -vi fred /etc/passwd

#same thing, case-insensitive
-Stands for
globally search a regular expression and print
-a command-line utility
-a text manipulation program used to find patterns in file or text
-was originally developed for the
Unix operating system
but is available today for all Unix-like systems.
>>Show additional six lines after matching
\ Quote the next metacharacter
^ Match the beginning of the line
. Match any character (except newline) = [^\n]
$ Match the end of the line (or before newline at the end)
| Alternation
( ) Grouping
[ ] Character class
{ } Match m to n times
* Match 0 or more times
+ Match 1 or more times
? Match 1 or 0 times

Meta Chars
^ Match the beginning of the line
$ Match the end of the line (or before newline at the end)
\A Matches only at the very beginning
\z Matches only at the very end
\Z Matches like $ used in single-line mode
\b Matches when the current position is a word boundary
\<,\> Matches when the current position is a word boundary
\B Matches when the current position is not a word boundary

Anchor matches a certain position in the subject string and it won’t consume any characters.

Quantifiers ( repetition) :

{m, n} = Matches minimum of m and a max of n occurrences.

* = {0,} = Matches zero or more occurrences ( any amount).

+ = {1,} = Matches one or more occurrences.

? = {0,1} = Matches zero or one occurrence ( means optional ).

Why? – Because we are not sure about text. Specifies how many times regex component must repeat.

\w \d \s word/digit/whitespace
[0-9] Matches any one of 0,1,2,3,4,5,6,7,8,9.
[aeiou] Matches one English vowel char.
[^aeiou] Matches any non-vowel char.
[a-z-] Matches a to z and ‘-’
[a-z0-9] Union matches a to z and 0 to 9.
[a-z&&[m-z]] Intersection matches m to z.
[a-z-[m-z] Subtraction matches a to l.

Character Classes – [ ]
Q & A

Thank you All !

Created by :

-First appearance in
Version 4 Unix
-Was created by
ken Thompson
as a
standalone application
-It was adapted from the Regular expression parser he had written for
editor, which he also created)
Relations Between grep & Regular Expression
: grep "REGEX" Filename
Supports 3 different versions of Regular Expression Syntax :
(ERE) and
c k s

regular expression :
no match

UNIX Tools is okay.


UNIX Tools sucks.


UNIX Tools rocks.

match 3

b [eor] a t

regular expression

match 2

match 1

beat a brat on a boat

Character classes [] can be used to match any
specific set of characters.

Character Classes

b [^eo] a t
regular expression :
beat a brat on a boat

Character classes can be negated with the [^] syntax.

Negated Character Classes
o a * o
regular expression :

For me to loop on.
regular expression : ya*y
I got mail, yaaaaaaaaaay!

a . * e
regular expression :
Scrapple from the apple.

If you want to count of a particular word in log file
you can use grep -c option to count the word.
Below command will print how many times
word "Error" has appeared in logfile.txt

grep -c "Error" logfile.txt
grep --context=6 successful logfile.txt
grep -C 2 'hello' *
>>Prints two lines of context
around each matching line
grep -w ERROR logfile
>>Above grep command in UNIX searches only for instances of 'ERROR' that are entire words; it does not match `SysERROR'.
grep -w 'ERROR>' *
Searches only for words ending in 'ERROR', so it matches the word `SysERROR'.
If you want to see line number of matching lines you
can use option "grep -n" below command will show on which lines Error has appeared.
grep -n ERROR log file.

3.Tareq - 011123035
Full transcript