Sunteți pe pagina 1din 14

grep/egrep/fgrep

grep

Where does the name come from?


:g/RE/p
globally regular expression print

How it works?

grep looks inside file(s) and returns any line that


contains the string or expression
prints lines matching a pattern to STDOUT

grep examples

grep Pattern
grep Pattern filename

grep pattern file1 file2

Ex. grep permission thisFile


Ex. grep permission file1 file2
file1:
file2:

If grep cannot find a line in any of the specified


files that contain the requested pattern, no output
is produced.

grep Exit Status

$? is set to 0

$? is set to 1

if grep finds nothing

$? is set to 2

if grep finds something

if one of the input file cannot be found.

sed and awk do not use the exit status to


indicated the success or failure of locating a
pattern. They report failure only if there is a syntax
error in a command.

grep continue

The general format:

Reading from files

grep [options] PATTERN [FILE]

grep re *

Reading from STDIN

grep pattern

cat file | grep pattern


ls l | grep Jan

More examples
A=This is it
echo $A | grep This
A=`grep tborrelli /etc/passwd`
if [[ -z $A ]] ; then
echo tborrelli Not found
else
echo tborrelli found
fi

grep Options

grep
-i: ignore case

-n: list line numbers along with the matching line

grep i unix filename


[File:]lineNumber:theLine

-v: invert match

grep v # /etc/hosts

Produces a list of all the lines in /etc/hosts that do not contain


the # character.

ps ef | grep tborrelli | grep v grep

grep Options cont

-l: only listing the filenames

grep l delete projects/*

-w: matches the whole word

grep w wordonly FILE


grep w north datafile

-c: Print the number of lines where the pattern was found

Pqops.c
Pqops.h
Scheduler.c

grep c ^no[a-z]* datafile

-A [B] num: Print num of lines after [before] match lines

grep A 1 ^south[a-z] datafile

The grep family

grep

egrep (grep E)

Extended/Enhanced grep with more RE metacharacters


egrep ^[0-9]+$

fgrep (grep F)

grep ^[0-9][0-9]*$
Try grep ^[0-9]+$
Try grep ^[0-9]\+$

Fixed/fast grep, treats all characters as literals


fgrep ^[0-9]+$

rgrep (grep r/R)

Recursive grep

fgrep

Special case: fgrep


fgrep STRING file1 file2

Same as grep -F STRING file1 file2


The STRING is a fixed string, not a REGEXP
All metacharacters are disabled i.e. they are treated
as themselves

egrep

Special case: egrep


egrep REGEXP file1 file2
Same

as egrep REGEXP file1 file2


The meta characters ?, +, {, |, (, and ) have their
special meanings

In grep (as opposed to egrep) these have no special


meaning unless escaped with a backslash \

egrep

Metacharacter
+, ?, a|b, ( )

Examples (copy the datafile from /home/fac/pub)


egrep

NW|EA datafile
egrep 3+ datafile
egrep 2\.?[0-9] datefile
egrep (no)+ datafile
egrep S(h|u) datafile
egrep Sh|u datafile
egrep [[:space:]]\.[[:digit:]][[:space:]] datafile

egrep (Cont)

egrep ^$ file
egrep fun\.$ file
egrep [A-Z][0-9] file
egrep v [tT]est file
egrep i sam file
egrep l Dear Boss *
egrep n $name file

The grep family (Cont)

GNU grep

grep
egrep or grep E
fgrep or grep F

More metacharacters

\w same as [a-zA-Z0-9_]
\W same as [^a-zA-Z0-9_]

S-ar putea să vă placă și