Search for lines in a file containing a pattern.
grep pattern files...
It prints each line in the file that matches the pattern. You won't believe how useful this is till you have a lot of files...
Options include
if grep -s $service /etc/services
then echo $service is a standard TCP service
fi
grep -l "important text" *
who | grep -c $USER
The expressions used by grep can be any regular expressions
Arithmetic operations and string comparison
expr x arith-op y
n=`expr $n + 1`
x=`expr 2 \* 3`
expr x rel-op y
if expr $x \< $y > /dev/null
then ...
expr string : regexp
if expr "$USER" : root > /dev/null
then echo user is root
else echo user is an ordinary user
fi
Other operations are also available, these are the major ones.
Stream editor. This is useful for on the fly editing, typically of small strings.
sed 1,10d file
prints file with lines 1-10 deleted
sed 20q file
prints first 20 lines and then quits
sed -n 20,30p file
-n turns off default printing, so only prints lines 20 to 30
sed 's/old/new/' file
prints file with occurrences of ``old'' changed to ``new''.
Both old and new can be regular expressions.
If no file is given, sed reads from standard input e.g. to remove the first header line from ps output
ps | sed 1d
The Turbo Pascal editor allows Ctrl-A to stand for any single character.
The Unix utilities grep, ed, vi, sed, awk, emacs etc., all support a particular type of pattern.
This is also available from C using the regexp or regex libraries, and is available in some other languages such as tcl. Perl is commonly used for CGI programs and makes heavy use of regular expressions.
Because the utilities grep and sed are often used in Unix shell programs it is worth looking at their pattern mechanism.
The simplest patterns are
^The$
matches ``The'' on a line by itself.
[Tt]he
matches ``The'' or ``the''
[0-9]*
matches any number
990[34][0-9]*
matches Monash phone numbers
-rw-r--r-- 1 jan 2048 Jul 4 file1To just extract the permissions part, use
ls -l | sed 's/ .*//'
More complicated patterns are
^\([^.]*\)\.\(.*\)$Then the same patterns reversed are
\2\1
For example, to change ``John.Smith'' to ``Smith, John'':
echo "John.Smith" | sed 's/^\([^.]*\)\.\(.*\)$/\2, \1/'