Unix Talk #2 grep/egrep/fgrep (maybe add more to this one….)
grep Where does the name come from? How it works? :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 In vi, try :/Line/p Grep command globally searches for regular expressions in files and prints all lines that contain the expression. The egrep is an extended grep, supporting more regular expression metacharacters The fgrep command called fixed grep, or fast grep, treats all characters as literals. That is, regular expression metacharacters aren’t special.
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. If grep can not find anything, $?=1
grep Exit Status $? is set to 0 $? is set to 1 $? is set to 2 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 grep [options] PATTERN [FILE…] The general format: 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 # get just the status in a variable: A=`(grep thisas a > /dev/null 2>&1);echo $?`
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 egrep (grep –E) fgrep (grep –F) rgrep (grep –r/R) 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 Same as grep -F STRING file1 file2 … 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 Examples (copy the datafile from /home/fac/pub) +, ?, 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 [:space:] newlines, spaces, tabs
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_]