Académique Documents
Professionnel Documents
Culture Documents
------------------- -------------------------
UNIX filter programs such as grep, awk and lex. They look similar to
the 'glob' filename constructs but have a separate syntax and work on
'text strings' rather than file names. The use of regular expression
NOTE: DOS system users can become familiar with regular expressions by
adding clone utilities of grep and awk. These can be downloaded from
The Metacharacters
------------------
Most punctuation characters have a meaning other than their literal value.
If you need to use the literal value, preceed the metacharacter with a slash.
------------------------
----------------------------------------
----------------------------
-------------------------------
Precedence
----------
-------------------------
Consider the regular expression for a quoted string which normally can't
\".*\" does not work as .* will find the closing quote and not leave
------------------------
(magna cum laude) is awarded for stating the cases it fails on and
----
These pattern searches are line oriented and do not wrap. An example of
a simple search is grep "the" text file | more. This search is case
sensitive and will find more than the word 'the' such as therefore.
To make it a bit more productive use grep -i "the " ie case insensitive
finds with the space in the pattern. Note that pipes and redirection
file.
grep allows fast pattern matching when you are not using metacharacters.
for example fgrep "billy" or grep -F "billy" will return the same results
awk
---
The awk (and its GNU clone gawk) filter/report generator utility provides
A program written in awk accepts input either from the standard input or
redirected from data files. awk processes input on a record by record
total 254
This command reads input piped from the ls utility and displays it.
<>awk program can be given on the command line within single forward quotes.
<>awk accepts input from standard input and displays to standard output.
NOTE: Each field in a record is referred to as $1, $2, $3, and so on,
would print fields five and nine of the data piped from ls.
awk program one:
!/bin/awk -f
Save this file and change its permission so that this program can be run.
$ ls -l | ./first.awk
Finished processing
the program have a condition on the left side, whereas the right side has
the statements to be run if the condition is true. The patterns BEGIN and
END are used to capture control before the first input line has been read
and after the last input line has been read respectively. These keywords
do not combine with any other patterns. The third line is an example of
FS Field separator
RS Record separator
If the pattern is found, then the action statements are executed. If there
is of the form:
expression op expression
== equals
!= not equal to
~ contains
line.
{ and } braces.
-------------------
length > 72
{ print $2, $1 }
ls -l | awk ' { s += $5 }
END { print "sum is", s, " average is", s/NR }'
Display Username, login, userid and groupid from the password file:
Scan the password file and get the next available userid:
#!/bin/ksh
PASSWDFILE=/etc/passwd
sort -t : -n +2 -3 $PASSWDFILE |
BEGIN { previous = 0 ; }
$3 > 100 {
print previous + 1 ;
previous = $3 ;
}'