140 likes | 158 Views
Understand grep, egrep, fgrep and their functionalities in Unix. Learn how to use patterns, options, and metacharacters effectively with practical examples. Uncover the origins and workings of these powerful search tools.
E N D
Unix Talk #2 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 • Ex. grep ‘permission’ thisFile • grep pattern file1 file2 • 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 • if grep finds something • $? is set to 1 • if grep finds nothing • $? is set to 2 • 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: • grep [options] PATTERN [FILE…] • Reading from files • 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 • grep –i unix filename • -n: list line numbers along with the matching line • [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/* • Pqops.c • Pqops.h • Scheduler.c • -w: matches the whole word • grep –w wordonly FILE • grep –w north datafile • -c: Print the number of lines where the pattern was found • 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 • grep ‘^[0-9][0-9]*$’ • Try grep ‘^[0-9]+$’ • Try grep ‘^[0-9]\+$’ • egrep (grep –E) • Extended/Enhanced grep with more RE metacharacters • egrep ‘^[0-9]+$’ • fgrep (grep –F) • 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 (Con’t) • 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 (Con’t) • 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_]