Professional Documents
Culture Documents
The data may come from standard input, one or more files, or as
output from a process
Awk scans a file (or input) line by line, from the first to the last line,
searching for lines that match a specified pattern and performing
selected actions (enclosed in curly braces) on those lines.
If there is a pattern with no specific action, all lines that match the
pattern are displayed;
patterns,
actions, or
!ormat"
nawk 'pattern' filename
nawk '{action}' filename
nawk 'pattern {action}' filename
Input from !iles
#ample $"
$ cat employees
Chen Cho 5/19/63 203-344-1234 $6
!om "ill# 4/12/45 913-92-4536 $102
$arr# %hite 11/2/54 90&-65-23&9 $54
"ill Clinton 1/14/60 654-56-4114 $201
'te(e )nn 9/15/1 202-545-&&99 $5&
$ nawk '/Tom/' employees
!om "ill# 4/12/45 913-92-4536 $102
$
Input from !iles (continue.)
#ample %"
$ cat employees
Chen Cho 5/19/63 203-344-1234 $6
!om "ill# 4/12/45 913-92-4536 $102
$arr# %hite 11/2/54 90&-65-23&9 $54
"ill Clinton 1/14/60 654-56-4114 $201
'te(e )nn 9/15/1 202-545-&&99 $5&
$ nawk '{print $1}' employees
Chen
!om
$arr#
"ill
'te(e
Awks format (continue.)
#ample &"
$ cat employees
Chen Cho 5/19/63 203-344-1234 $6
!om "ill# 4/12/45 913-92-4536 $102
$arr# %hite 11/2/54 90&-65-23&9 $54
"ill Clinton 1/14/60 654-56-4114 $201
'te(e )nn 9/15/1 202-545-&&99 $5&
$ nawk '/Steve/{print $1, $2}' employees
'te(e )nn
"he print function
variables,
computed values, or
string constants
#ample"
$ date
*ri *e+ 9 0,49,2& -'! 2001
$ date | nawk '{ print "Mont! " $2
""n#ear! ", $$}'
.onth, *e+
/ear, 2001
'scape se$uences
#ample"
$ cat employees
Chen Cho 5/19/63 203-344-1234 $6
!om "ill# 4/12/45 913-92-4536 $102
$arr# %hite 11/2/54 90&-65-23&9 $54
"ill Clinton 1/14/60 654-56-4114 $201
'te(e )nn 9/15/1 202-545-&&99 $5&
$ nawk '/%nn/{print ""t"t&ave a nice day, " $1,
$2 ""'"}' employees &ave a nice day, Steve %nn'
"he printf !unction
#ample $"
$ eco "()*+" | nawk ' {print, "|-.1/s|"n",
$1}'
01234 0
$ eco "()*+" | nawk '{print, "|-1/s|"n",
$1}'
0 12340
$
"he printf !unction (continue.)
#ample %"
$ cat employees
Chen Cho 5/19/63 203-344-1234 $6
!om "ill# 4/12/45 913-92-4536 $102
$arr# %hite 11/2/54 90&-65-23&9 $54
"ill Clinton 1/14/60 654-56-4114 $201
'te(e )nn 9/15/1 202-545-&&99 $5&
$ nawk '{print, "Te name is! -.1/s *0 is
-1d"n", $1, $2}' employees
!he name i5, Chen 36 i5 5
!he name i5, !om 36 i5 4
!he name i5, $arr# 36 i5 11
!he name i5, "ill 36 i5 1
!he name i5, 'te(e 36 i5 9
$
"he printf !unction (continue.)
%on&ersion
%haracter
(efnition
c ,haracter
s 4tring
d 5ecimal number
ld 6ong decimal number
u 2nsigned decimal
number
lu 6ong unsigned decimal
number
"he printf !unction (continue.)
%on&ersion %haracter (efnition
7eadecimal number
l 6ong headecimal number
o 0ctal number
lo 6ong octal number
e !loating point number in
scientifc notation (e8
notation)
f !loating point number
g !loating point number using
either e or f conversion,
whichever ta'es the least
space
"he printf !unction (continue.)
)odifer %haracter (efnition
8 6eft89ustifcation modifer
: ;ntegers in octal form are
displayed with a leading -<
integers in headecimal
form are displayed with a
leading -.
= !or conversions using d,e,f,
and g, integers are
displayed with a numeric
sign = or 8
- The displayed value is
padded with >eros instead of
white space
"he printf !unction (continue.)
Printf !ormat #pecifer What it (oes
?iven @AAB,y@$C,>@%.&, and D$ @ *ob 4mith"
Ec Prints a single A4,;; character.
printf("The character is %c\n",x) prints"
The character is A
Ed Prints a decimal number
printf("The !" is %d "ears !ld\n", ")
prints" The !" is #$ "ears !ld
Ee Prints the e notation of a number
printf("% is %f\n", %) prints" > is &'(e)*#
Ef Prints a Foating point number
printf("% is %e\n", %) prints" > is +',*****
"he printf !unction (continue.)
Printf !ormat
#pecifer
What it (oes
?iven @AAB,y@$C,>@%.&, and D$ @ *ob 4mith"
Eo Prints the octal value of a number
printf("" is %!\n",") prints" " is #,
Es Prints a string of characters
printf("The na-e !f the c.lprit is
%s\n", /#) prints" The na-e !f the
c.lprit is 0! S-ith
E Prints the hex 1al.e of a number
printf("" is %x\n", ") prints" " is F
awk commands from within
a
fle (continue.)
#ample"
Chen Cho 5/19/63 203-344-1234 $6
!om "ill# 4/12/45 913-92-4536 $102
$arr# %hite 11/2/54 90&-65-23&9 $54
"ill Clinton 1/14/60 654-56-4114 $201
'te(e )nn 9/15/1 202-545-&&99 $5&
$ cat awk,ile
/3Steve/{print "&ello Steve'"}
{print $1, $2, $2}
$ nawk ., awk,ile employees
Chen Cho 5/19/63
!om "ill# 4/12/45
$arr# %hite 11/2/54
"ill Clinton 1/14/60
7ello 'te(e8
'te(e )nn 9/15/1
*ecords
#ample"
$ cat employees
Chen Cho 5/19/63 203-344-1234 $6
!om "ill# 4/12/45 913-92-4536 $102
$arr# %hite 11/2/54 90&-65-23&9 $54
"ill Clinton 1/14/60 654-56-4114 $201
'te(e )nn 9/15/1 202-545-&&99 $5&
$ nawk '{print $4}' employees
Chen Cho 5/19/63 203-344-1234 $6
!om "ill# 4/12/45 913-92-4536 $102
$arr# %hite 11/2/54 90&-65-23&9 $54
"ill Clinton 1/14/60 654-56-4114 $201
'te(e )nn 9/15/1 202-545-&&99 $5&
"he 43 +ariable
#ample"
$ cat employees
Chen Cho 5/19/63 203-344-1234 $6
!om "ill# 4/12/45 913-92-4536 $102
$arr# %hite 11/2/54 90&-65-23&9 $54
"ill Clinton 1/14/60 654-56-4114 $201
'te(e )nn 9/15/1 202-545-&&99 $5&
$ nawk '{print )5, $4}' employees
1 Chen Cho 5/19/63 203-344-1234 $6
2 !om "ill# 4/12/45 913-92-4536 $102
3 $arr# %hite 11/2/54 90&-65-23&9 $54
4 "ill Clinton 1/14/60 654-56-4114 $201
5 'te(e )nn 9/15/1 202-545-&&99 $5&
!ields
#ample $"
$1 $2 $3 $4 $5
Chen Cho 5/19/63 203-344-1234 $6
!om "ill# 4/12/45 913-92-4536 $102
$arr# %hite 11/2/54 90&-65-23&9 $54
"ill Clinton 1/14/60 654-56-4114 $201
'te(e )nn 9/15/1 202-545-&&99 $5&
$ nawk '{print )5, $1, $2, $/}' employees
1 Chen Cho $6
2 !om "ill# $102
3 $arr# %hite $54
4 "ill Clinton $201
5 'te(e )nn $5&
!ields (continue.)
#ample %"
nawk '{print $4, )6}' employees
Chen Cho 5/19/63 203-344-1234 $6 5
!om "ill# 4/12/45 913-92-4536 $102 5
$arr# %hite 11/2/54 90&-65-23&9 $54 5
"ill Clinton 1/14/60 654-56-4114 $201 5
'te(e )nn 9/15/1 202-545-&&99 $5& 5
"he Input !ield #eparator
)xample'
$ cat employees
Chen Cho,5/19/63,203-344-1234,$6
!om "ill#,4/12/45,913-92-4536,$102
$arr# %hite,11/2/54,90&-65-23&9,$54
"ill Clinton,1/14/60,654-56-4114,$201
'te(e )nn,9/15/1,202-545-&&99,$5&
$ nawk .6! '/Tom 7illy/{print $1, $2}'
employees
!om "ill# 4/12/45
,sing )ore than -ne !ield
#eparator
)xample
$ nawk .6'8 !"t9' '{print $1, $2, $2}: employees
Chen Cho 5/19/63
!om "ill# 4/12/45
$arr# %hite 11/2/54
"ill Clinton 1/14/60
'te(e )nn 9/15/1
"he -utput !ield #eparator
)xample'
$ cat employees
Chen Cho,5/19/63,203-344-1234,$6
!om "ill#,4/12/45,913-92-4536,$102
$arr# %hite,11/2/54,90&-65-23&9,$54
"ill Clinton,1/14/60,654-56-4114,$201
'te(e )nn,9/15/1,202-545-&&99,$5&
$ nawk .6! '/Tom 7illy/{print $1 $2 $2 $;}'
employees
!om "ill#4/12/45913-92-4536$102
.atterns
, pattern consists of
(
a regular expression,
(
an expression resulting in a true or false condition, or
(
a combination of these
)xample'
$ cat employees
Chen Cho 5/19/63 203-344-1234 $6
!om "ill# 4/12/45 913-92-4536 $102
$arr# %hite 11/2/54 90&-65-23&9 $54
"ill Clinton 1/14/60 654-56-4114 $201
'te(e )nn 9/15/1 202-545-&&99 $5&
$ nawk '/Tom/' employees
!om "ill# 4/12/45 913-92-4536 $102
$ nawk '$; < ;4' employees
Chen Cho 5/19/63 203-344-1234 $6
'te(e )nn 9/15/1 202-545-&&99 $5&
Actions
)xample -'
)xample .'
$ nawk '/Steve/' employees
'te(e )nn 9/15/1 202-545-&&99 $5&
$ nawk '/Steve/{print $1, $2}' employees
'te(e )nn
*egular 'pression
)eta characters
5 (atches at the beginning of string
/ (atches at the end of string
' (atches for a single character
6 (atches >ero or more of preceding
character
) (atches for one or more of preceding
character
7 (atches for >ero or one of preceding
character
8A09: (atches for any one character in the set of
characters, i.e., A,0, or 9
*egular 'pression )eta characters
(continue.)
85A09: (atches characters not in the set of
characters, i.e.,A,0 or 9
8A2;: (atches for any character in the
range from A to ;
A<0 (atches either A or 0
(A0)) (atches one or more sets of A0
\6 (atches for a literal asteris'
= 2sed in the replacement to represent
what was found in the search string
*egular 'pressions (continue.)
)xample /'
)xample 0'
$ nawk '/3Steve/' employees
'te(e )nn 9/15/1 202-545-&&99 $5&
$ nawk '/38%.=98a.>9? /' employees
Chen Cho 5/19/63 203-344-1234 $6
!om "ill# 4/12/45 913-92-4536 $102
$arr# %hite 11/2/54 90&-65-23&9 $54
"ill Clinton 1/14/60 654-56-4114 $201
'te(e )nn 9/15/1 202-545-&&99 $5&
"he )atch -perator
)xample -'
$ cat employees
Chen Cho 5/19/63 203-344-1234 $6
!om "ill# 4/12/45 913-92-4536 $102
$arr# %hite 11/2/54 90&-65-23&9 $54
"ill Clinton 1/14/60 654-56-4114 $201
'te(e )nn 9/15/1 202-545-&&99 $5&
$ nawk '$1 @ /87A9ill/' employees
"ill Clinton 1/14/60 654-56-4114 $201
"he )atch -perator (continue.)
)xample .'
$ nawk '$1 '@ /lee$/' employees
Chen Cho 5/19/63 203-344-1234 $6
!om "ill# 4/12/45 913-92-4536 $102
$arr# %hite 11/2/54 90&-65-23&9 $54
"ill Clinton 1/14/60 654-56-4114 $201
'te(e )nn 9/15/1 202-545-&&99 $5&
awk %ommands in a #cript !ile
)xample'
$ cat employees
Chen Cho,5/19/63,203-344-1234,$6
!om "ill#,4/12/45,913-92-4536,$102
$arr# %hite,11/2/54,90&-65-23&9,$54
"ill Clinton,1/14/60,654-56-4114,$201
'te(e )nn,9/15/1,202-545-&&99,$5&
$ cat in,o
9 .# fir5t awk 5cript +# )+:el5hako;r )+;<nei:
9 'cript name, info= 6ate, *e+r;ar# 09> 2001
/!om/{print ?!om'5 +irth:a# i5 ?$3}
/"ill/{print 2@> $0}
/A'te(e/{print ?7i 'te(eB ? $1 ? ha5 a 5alar# of ? $4
?B?}
9-n: of info 5cript
awk %ommands in a #cript !ile
(continue.)
)xample (continue.)'
)xample'
$ cat employees
Chen Cho 5/19/63 203-344-1234 6
!om "ill# 4/12/45 913-92-4536 102
$arr# %hite 11/2/54 90&-65-23&9 54
"ill Clinton 1/14/60 654-56-4114 201
'te(e )nn 9/15/1 202-545-&&99 5&
$ nawk '$/ BB 241' employees
"ill Clinton 1/14/60 654-56-4114 201
$ nawk '$/ C 144' employees
!om "ill# 4/12/45 913-92-4536 102
$ nawk '$2 @ /%nn/ ' employees
'te(e )nn 9/15/1 202-545-&&99 5&
*elational -perators (continue.)
)xample (continue)'
$ nawk '$2 '@ /%nn/ ' employees
Chen Cho 5/19/63 203-344-1234 6
!om "ill# 4/12/45 913-92-4536 102
$arr# %hite 11/2/54 90&-65-23&9 54
"ill Clinton 1/14/60 654-56-4114 201
%onditional 'pressions
#ormat'
con:itional eCpre55ion1 D eCpre55ion2 , eCpre55ion3
%onditional 'pressions (continue.)
)xample'
$ nawk '{maDBE$1 C $2F G $1 ! $2H print maD}' employees
Cho
!om
%hite
Clinton
'te(e
%omputation
)xample'
$ nawk '$2 I $; C /44' ,ilename
%ompound .atterns
)xample '
$ nawk '$2 C / JJ $2 <B 1/' employees
$
$ nawk '$/ BB 1444 || $2 C /4' employees
'te(e )nn 9/15/1 202-545-&&99 5&
$
*ange .atterns
)xample '
$ nawk '/Tom/,/Steve/' employees
!om "ill# 4/12/45 913-92-4536 102
$arr# %hite 11/2/54 90&-65-23&9 54
"ill Clinton 1/14/60 654-56-4114 201
'te(e )nn 9/15/1 202-545-&&99 5&
$
/umeric and #tring %onstants
, !ariable can be
(
a string
(
a number, or
(
both
)xample '
$ nawk '$1 @ /Tom/ {waKe B $/ I ;4H print
waKe}' employees
40&0
Increment and (ecrement
-perators
)xample'
$ nawk F: -f awkscript month=4 year=1999
filename
"he v -ption (nawk)
)xample'
$ nawk .6! '$1 BB "Steve %nn"{print )5, $1, $2,
$)6}' employees2
5 'te(e )nn 9/15/1 $5&
0HDF4 .atterns
)xample - '
$ nawk '7LM*){6SB"!"H N6SB""t"H N5SB""n"n"}
{print $1,$2,$2}' employees2
Chen Cho 5/19/63 203-344-1234
!om "ill# 4/12/45 913-92-4536
$arr# %hite 11/2/54 90&-65-23&9
"ill Clinton 1/14/60 654-56-4114
'te(e )nn 9/15/1 202-545-&&99
$
0HDF4 .atterns (continue.)
)xample . '
$ nawk '7LM*){print "Make #ear"}'
.ake /ear
H4L .atterns
EN# patterns do not match any input lines, but executes any
actions that are associated with the EN# pattern. EN# patterns
are handled af$er all lines of input ha!e been processed
)xamples'
$ nawk 'L)0{print "Te nOmAer o, records is "
)5 }' employees
!he n;m+er of recor:5 i5 5
$ nawk '/Steve/{coOnt??}L)0{print "Steve was ,oOnd
" coOnt " timesP"}' employees
'te(e wa5 fo;n: 1 time5B
$
-utput *edirection
)xample'
$ nawk '$5 >= 70 {print $1, $2 > "passingfile" !' emplo"ees
$ cat passingfile
2hen 2ho
Tom <illy
<ill 2linton
"he getline !unction
It gets the next line of input and sets the NF, N" and the
FN" builtin !ariables
)xamples '
$ nawk '7LM*){ "date" | Ketline dH print d}'
employees2
*ri *e+ 9 09,39,53 -'! 2001
$ nawk '7LM*){ "date" | Ketline dH splitE d, monFH
print mon829}' employees
*e+
$ nawk '7LM*){wileE"ls" | KetlineF print}
1234
(arfile
(arfile2
(arfile3
(arfile4
(arfile5
(arfile6
"he getline !unction (continue.)
)xamples '
$ nawk '7LM*){ print "Qat is yoOr nameG" H"
C Ketline name < "/dev/tty"}"
C $1 @ name {print "6oOnd " name " on line ",
)5 "P"}"
C L)0{print "See ya, " name "P"}' employees
%hat i5 #o;r nameD
a+:;l
'ee #a> a+:;lB
$
.ipes
)xample'
$ cat names
Ehon 5mith
alice che+a
ton# tram
:an 5a(aFe
eli<a Fol:+orF
$ nawk '{print $1, $2 | "sort .r ?1 .2 ?4 .1
"}' names
ton# tram
Ehon 5mith
:an 5a(aFe
eli<a Fol:+orF
alice che+a
$
%losing !iles and .ipes
)xample'
! "n script#
{ print $1, $%, $3 & sort 'r +1 '% +( '1}
)N*{
+lose!sort 'r +1 '% +( '1#
,rest o- statement. }
"he #ystem !unction
)xample'
! "n script#
{
/ystem ! cat $1 #
/ystem ! clear #
}
Ff #tatement
#ormat'
If )expression* +
s$a$e(en$, s$a$e(en$, -
.
FfCelse #tatement
#ormat'
+If )expression* +
s$a$e(en$, s$a$e(en$, -
.
else +
s$a$e(en$, s$a$e(en$, -
.
.
FfCelse #tatement
)xample'
$ nawk '{i,E$$ C /4F print $1 "Too iK"H "
C else print "5anKe is NR"}' names
@anFe i5 GH
@anFe i5 GH
@anFe i5 GH
@anFe i5 GH
@anFe i5 GH
$
FfCelse else if #tatement
#ormat'
+ If )expression* +
s$a$e(en$, s$a$e(en$, -
.
else if )expression* +
s$a$e(en$, s$a$e(en$, -
.
else if )expression* +
s$a$e(en$, s$a$e(en$, -
.
else +
s$a$e(en$, s$a$e(en$, -
.
.
3oops
)xample'
$ nawk '{ i B 1H wile Ei <B )6 F { print )6,
$iH i??}}' names
2 Ehon
2 5mith
2 alice
2 che+a
2 ton#
2 tram
2 :an
2 5a(aFe
2 eli<a
2 Fol:+orF
$
f!r 3oop
)xample'
$ nawk '{ i B 1H wile Ei <B )6 F { print )6,
$iH i??}}' names
2 Ehon
2 5mith
2 alice
2 che+a
2 ton#
2 tram
2 :an
2 5a(aFe
2 eli<a
2 Fol:+orF
$
reak and c!ntin.e #tatement
)xample'
(In $cript)
A if (B- CeterD AnextDD
else AprintD
D
next #tatement
The nex$ statement gets the next line of input from the
input file, restarting execution at the top of the awk script
)xample'
(In $cript)
- Afor ( x E /; x FE 6#; xGG)
if ( Bx F 4) Aprint H<ottomed outIJ; #reakD
3 brea&s out of the loop
D
. Afor ( x E /; x FE 6#; xGG )
if ( Bx EE 4 ) A print H=et next itemJ; contin$eD
3 starts next iteration of the for loop
D
exit #tatement
)xample'
)In S3rip$*
+exi$ )1*.
)4/e 5o((an0 6ine*
7 e3/o 8s$a$us )3s/*
1
8 e3/o 89 )s/1ks/*
1
Arrays
)xample'
$ cat employees
Chen Cho 5/19/63 203-344-1234 6
!om "ill# 4/12/45 913-92-4536 102
$arr# %hite 11/2/54 90&-65-23&9 54
"ill Clinton 1/14/60 654-56-4114 201
'te(e )nn 9/15/1 202-545-&&99 5&
$ nawk '{name8D??9B$2}HL)0{,orEiB4H i<)5Hi??F"
C print i, name8i9}' employees
0 Cho
1 "ill#
2 %hite
3 Clinton
4 )nn
Arrays (continue.)
)xample'
$ nawk '{id8)59B$2}HL)0{,orED B 1H D<B )5H
D??F"
C print id8D9}' employees
5/19/63
4/12/45
11/2/54
1/14/60
9/15/1
$
"he special f!r 3oop
)xample'
$ cat dA
!om Ione5
.ar# ):am5
'all# ChanF
"ill# "lack
!om 'a(aFe
$ nawk '/3Tom/{name8)59B$1}H"
C L)0{,orE iB1H i <B )5H i?? Fprint name8i9}' dA
!om
!om
$
"he special f!r 3oop (continue.)
)xample'
$ nawk '/3Tom/{name8)59B$1}H"
C L)0{,orEi in nameF{print name8i9}}' dA
!om
!om
$
,sing #trings as Array #ubscripts
)xample
$ cat dA
!om Ione5
.ar# ):am5
'all# ChanF
"ill# "lack
!om 'a(aFe
$ nawk ., awkscript dA
!here are 2 !om'5 in the file an: 1 .ar#'5 in the fileB
,sing !ield +alues as Array
#ubscripts (continue.)
)xample -'
$ cat dA
!om Ione5
.ar# ):am5
'all# ChanF
"ill# "lack
!om 'a(aFe
$ nawk '{coOnt8$29??}L)0{,orEname in coOntFprint name,
coOnt8name9 }' dA
ChanF 1
"lack 1
Ione5 1
'a(aFe 1
):am5 1
$
,sing !ield +alues as Array
#ubscripts (continue.)
)xample .'
$ cat dA
!om Ione5
.ar# ):am5
'all# ChanF
"ill# "lack
!om 'a(aFe
$ nawk '{coOnt8$29??}L)0{,orEname in coOntFprint name,
coOnt8name9 }' dA
ChanF 1
"lack 1
Ione5 1
'a(aFe 1
):am5 1
$
Arrays and the split !unction
#ormat'
split!string, array, -ield separator#
split!string, array#
"he delete !unction
)xample'
$ cat myscript
STis script is called myscript
7LM*){
,or E i B 4H i< %5MTH i??F{
print,E"arKv8-d9 is -s"n", i,
%5MU8i9F
}
print,E"Te nOmAer o, arKOments, %5MTB-d"n",
%5MTF
}
A3D9 (continue.)
)xample '
$ cat %5MUS
STis script is called arKvs
7LM*){
,or E iB4H i < %5MTH i?? F {
print,E"arKv8-d9 is -s"n", i, %5MU8i9F
}
print,E"Te nOmAer o, arKOments, %5MTB-d"n", %5MTF
}
$
$ nawk ., %5MUS data,ile
arF(J0K i5 nawk
arF(J1K i5 :atafile
!he n;m+er of arF;ment5> )@LCM2
$
A3D9 (continue.)
)xample '
$ nawk ., %5MUS data,ile "Veter Van" 12
arF(J0K i5 nawk
arF(J1K i5 :atafile
arF(J2K i5 Neter Nan
arF(J3K i5 12
!he n;m+er of arF;ment5> )@LCM4
$
A3D9 (continue.)
)xample '
$ cat arKinK
STis script is called arKin
7LM*){6SB"!"H nameB%5MU829
print "%5MU829 is "%5MU829
}
$1 @ name { print $4 }
$ nawk ., arKinK employees2 "Ten To"
)@LOJ2K i5 Chen Cho
Chen Cho,5/19/63,203-344-1234,$6
nawk, can't open file Chen Cho
inp;t recor: n;m+er 5> file Chen Cho
5o;rce line n;m+er 1
A3D9 (continue.)
)xample '
$ cat arKinK1
STis script is called arKin
7LM*){6SB"!"H nameB%5MU829
print "%5MU829 is "%5MU829
delete %5MU829
}
$1 @ name { print $4 }
$ nawk ., arKinK1 employees2 "Ten To"
)@LOJ2K i5 Chen Cho
Chen Cho,5/19/63,203-344-1234,$6
"he s. and gs. !unctions
#ormat'
5;+ PreF;lar eCpre55ion> 5;+5tit;tion 5trinFQ=
5;+ PreF;lar 5trinF> 5;+5tit;tion 5trinF>
tarFet 5trinFQ
#ormat'
F5;+ PreF;lar eCpre55ion> 5;+5tit;tion 5trinFQ
5;+ PreF;lar 5trinF> 5;+5tit;tion 5trinF>
tarFet 5trinFQ
"he index !unction
#ormat'
lenFth P5trinFQ
lenFth
"he length !unction
)xample
$ nawk '{ print lenKtE"ello"F }' employees
5
5
5
5
5
$
"he s.str !unction
#ormat'
5;+5tr P5trinF> 5tartinF po5itionQ
5;+5tr P5trinF> 5tartinF po5ition>
lenFth of 5trinF
"he -atch !unction
#ormat'
match P5trinF> reF;lar eCpre55ionQ
"he -atch !unction (continue.)
)xample '
$ nawk 'L)0{startBmatcE"Mood ole (S%", /8%.=9
?$/FH"
C print 5ST%5T, 5WL)MT&}' employees
10 3
"he split !unction
#ormat'
5plit P5trinF> arra#> fiel: 5eparatorQ
5plit P5trinF> arra#Q
"he sprintf !unction
#ormat'
Oaria+leM5printfPR5trinF with format
5pecifier5S eCpr1> eCpr2> T> eCprnQ
"he sprintf !unction (continue.)
)xample '
$ awk '{line B sprint, E "-.1/s -$P2, ", $1 ,
$2 FH"
C print line}' employees
Chen 0B00
!om 0B00
$arr# 0B00
"ill 0B00
'te(e 0B00
2uilt1in Arithmetic
!unctions
/ame +alue returned
atan%(,y) Arctangent of yM in the
range
cos() ,osine of , with in
radians
ep() #ponential function of ,
e
int() ;nteger part of <
truncated toward - when
K-
log() +atural (ase e) logarithm
of
rand() Sandom number r, where
-IrI$
2uilt1in Arithmetic !unctions
(continue.)
/ame +alue returned
sin() 4ine of , with radians
sqrt() 4quare root of
srand() T is a new seed for
rand()
Fnteger !unction
)xample
$ nawk '{print randEF}' employees
0B513&1
0B1526
0B30&634
0B534532
0B9463
$ nawk '{print randEF}' employees
0B513&1
0B1526
0B30&634
0B534532
0B9463
"he srand !unction
sran0)x* uses x as the seed. 6ormally, x should !ary during the run
of the program
)xample
$ nawk '7LM*){srandEF}H{print randEF}' employees
0B54&53
0B392254
0B9242
0B&2149
0B15322
$
"he srand !unction (continue.)
)xample
$ nawk '7LM*){srandEF}H{print randEF}' employees
4P/;1X/2
4P2Y22/;
4PYX2;X2
4P121;YX
4P1/2X22
$ nawk '7LM*){srandEF}H{print randEF}' employees
4P44/2X12$
4P212X1;
4P22X22
4P122422
4P1X24;
,ser1(efned !unctions (nawk)
#ormat
f;nction name Pparameter> parameter>
parameter> TQ{
5tatement5
ret;rn eCpre55ion
Pthe ret;rn 5tatement an: eCpre55ion
are optionalQ
}
,ser1(efned !unctions (nawk)
(continue.)
)xample
$ cat Krades
44 55 66 22 99
100 22 99 33 66
55 66 100 99 && 45
$
,ser1(efned !unctions (nawk)
(continue.)
)xample
$ cat sorter
SScript is called sorter
S *t sorts nOmAers in ascendinK order
,Onction sort E scores, nOmZelements, temp, i, [ F
{
S temp, i, and [ will Ae local and private,
Swit an initila valOe o, nOllP
,orE i B 2H i <B nOmZelements H i?? F
{
,or E [ B iH scores 8[.19 C scores8[9H ..[F
{
temp B scores8[9
scores8[9 B scores8[.19
scores8[.19 B temp
}
}
}
,ser1(efned !unctions (nawk)
(continue.)
)xample
)xample
$ cat ,ile
CCC CCC
CCC a+c CCC
CCC a +++
CCC CC
'mpty !ields (continue.)
$ cat awk,iD
SVreservinK empty ,ieldsP 6ield widt is ,iDedP
{
,819BsOAstrE$4,1,2F
,829BsOAstrE$4,/,2F
,829BsOAstrE$4,Y,2F
lineBsprint,E"-.;s-.;s-.;s"n", ,819, ,829, ,829F
print line
}
$ nawk ., awk,iD ,ile
CCC CCC
CCC a+c CCC
CCC a +++
CCC CC
'mpty !ields (continue.)