You are on page 1of 11

1.

--> delete every 2nd field in file


awk '{$2= ""; print}' datafile > datafile.new
2.
1. Renaming within the name:
ls -1 *old* | awk '{print "v "$1" "$1}' | sed s!old!new!2
| s"
(although in some cases it will fail, as in file_old_and_old)
2. Remove only files:
ls -l * | #rep -v drw$ | awk '{print "r "$%}' | s"
or with awk alone:
ls -l|awk '$1&'!(drw$!{print $%}'|$ar#s r
Be careful when trying this out in your home directory. We remove
files
!. Remove only directories
ls -l | #rep '(d' | awk '{print "r -r "$%}' | s"
or
ls -p | #rep !$ | wk '{print "r -r "$1}'
or with awk alone:
ls -l|awk '$1'!(d.*$!{print $%}'|$ar#s r -r
Be careful when trying this out in your home directory. We remove
things
awk ')*+,- { ./ = "0" } { print $1 | "sort" }' !et1!passwd

"his #rogram #rints a sorted list of the login names
of all users.
2 print t"e s3s of t"e fields of every line
awk '{s=4; for 5i=1; i6=-.; i778 s=s7$i;
print s}'
2 add all fields in all lines and print t"e
s3
awk '{for 5i=1; i6=-.; i778 s=s7$i};
*-9{print s}'
2 print every line after repla1in# ea1"
field wit" its a:sol3te val3e
awk '{for 5i=1; i6=-.; i778 if 5$i 6 48 $i =
-$i; print }'
awk '{for 5i=1; i6=-.; i778 $i = 5$i 6 48 ;
-$i 0 $i; print }'
2 print t"e total n3:er of fields 5"words"8
in all lines
awk '{ total = total 7 -. }; *-9 {print
total}' file
2 print t"e total n3:er of lines t"at
1ontain ")et""
awk '!)et"!{n77}; *-9 {print n74}' file
2 print t"e lar#est first field and t"e line
t"at 1ontains it
2 ,ntended for findin# t"e lon#est strin# in
field 21
awk '$1 > a$ {a$=$1; a$line=$4};
*-9{ print a$< a$line}'
2 print t"e n3:er of fields in ea1" line<
followed :y t"e line
awk '{ print -. "0" $4 } '
2 print t"e last field of ea1" line
awk '{ print $-. }'
2 print t"e last field of t"e last line
awk '{ field = $-. }; *-9{ print field }'
2 print every line wit" ore t"an = fields
awk '-. > ='
2 print every line w"ere t"e val3e of t"e
last field is > =
awk '$-. > ='
>*?> @A-B*C/,A- D-9 /E)/>,>E>,A-0
2 ,- E-,? *-B,CA-F*->0 1onvert 9A/ newlines
5@C!G.8 to Eni$ forat
awk '{s3:5!Hr$!<""8;print}' 2 ass3es *D@I
line ends wit" @trl-F
2 ,- E-,? *-B,CA-F*->0 1onvert Eni$ newlines
5G.8 to 9A/ forat
awk '{s3:5!$!<"Hr"8;print}
2 ,- 9A/ *-B,CA-F*->0 1onvert Eni$ newlines
5G.8 to 9A/ forat
awk 1
2 ,- 9A/ *-B,CA-F*->0 1onvert 9A/ newlines
5@C!G.8 to Eni$ forat
2 @annot :e done wit" 9A/ versions of awk<
ot"er t"an #awk0
#awk -v ),-FA9*="w" '1' infile >o3tfile
2 Ese "tr" instead.
tr -d Hr 6infile >o3tfile 2 +-E
tr version 1.22 or "i#"er
2 delete leadin# w"itespa1e 5spa1es< ta:s8
fro front of ea1" line
2 ali#ns all te$t fl3s" left
awk '{s3:5!(J HtK7!< ""8; print}'
2 delete trailin# w"itespa1e 5spa1es< ta:s8
fro end of ea1" line
awk '{s3:5!J HtK7$!< ""8;print}'
2 delete )A>I leadin# and trailin#
w"itespa1e fro ea1" line
awk '{#s3:5!(J HtK7|J HtK7$!<""8;print}'
awk '{$1=$1;print}' 2 also reoves
e$tra spa1e :etween fields
2 insert L :lank spa1es at :e#innin# of ea1"
line 5ake pa#e offset8
awk '{s3:5!(!< " "8;print}'
2 ali#n all te$t fl3s" ri#"t on a M%-1ol3n
widt"
awk '{printf "NM%sHn"< $4}' file*
2 1enter all te$t on a M%-1"ara1ter widt"
awk'{l=len#t"58;s=int55M%-l8!28; printf
"N"5s7l8"sHn"<$4}' file*
2 s3:stit3te 5find and repla1e8 "foo" wit"
":ar" on ea1" line
awk '{s3:5!foo!<":ar"8;print}' 2
repla1es only 1st instan1e
#awk '{$4=#ens3:5!foo!<":ar"<=8;print}' 2
repla1es only =t" instan1e
awk '{#s3:5!foo!<":ar"8;print}' 2
repla1es DGG instan1es in a line
2 s3:stit3te "foo" wit" ":ar" A-GO for lines
w"i1" 1ontain ":aP"
awk '!:aP!{#s3:5!foo!< ":ar"8};{print}'
2 s3:stit3te "foo" wit" ":ar" *?@*Q> for
lines w"i1" 1ontain ":aP"
awk '&!:aP!{#s3:5!foo!< ":ar"8};{print}'
2 1"an#e "s1arlet" or "r3:y" or "p31e" to
"red"
awk '{#s3:5!s1arlet|r3:y|p31e!< "red"8;
print}'
2 reverse order of lines 5e3lates "ta1"8
awk '{aJi77K=$4} *-9 {for 5R=i-1; R>=4;8
print aJR--K }' file*
2 if a line ends wit" a :a1kslas"< append
t"e ne$t line to it
2 5fails if t"ere are 3ltiple lines endin#
wit" :a1kslas"...8
awk '!HH$! {s3:5!HH$!<""8; #etline t; print
$4 t; ne$t}; 1' file*
2 print and sort t"e lo#in naes of all 3sers
awk -. "0" '{ print $1 | "sort" }'
!et1!passwd
2 print t"e first 2 fields< in opposite
order< of every line
awk '{print $2< $1}' file
2 swit1" t"e first 2 fields of every line
awk '{tep = $1; $1 = $2; $2 = tep}' file
2 print every line< deletin# t"e se1ond
field of t"at line
awk '{ $2 = ""; print }'
2 print in reverse order t"e fields of every
line
awk '{for 5i=-.; i>4; i--8 printf5"Ns
"<i8;printf 5"Hn"8}' file
2 reove d3pli1ate< 1onse13tive lines
5e3lates "3niS"8
awk 'a &' $4; {a=$4}'
2 reove d3pli1ate< non1onse13tive lines
awk '& aJ$4K77' 2 ost
1on1ise s1ript
awk '&5$4 in a8 {aJ$4K;print}' 2 ost
effi1ient s1ript
2 1on1atenate every L lines of inp3t< 3sin#
a 1oa separator
2 :etween fields
awk 'AC/=N-CNL;"<"0"Hn"' file
/*G*@>,B* QC,->,-+ A. @*C>D,- G,-*/0
2 print first 14 lines of file 5e3lates
:e"avior of ""ead"8
awk '-C 6 11'
2 print first line of file 5e3lates ""ead
-1"8
awk '-C>1{e$it};1'
2 print t"e last 2 lines of a file
5e3lates "tail -2"8
awk '{y=$ "Hn" $4; $=$4};*-9{print y}'
2 print t"e last line of a file 5e3lates
"tail -1"8
awk '*-9{print}'
2 print only lines w"i1" at1" re#3lar
e$pression 5e3lates "#rep"8
awk '!re#e$!'
2 print only lines w"i1" do -A> at1" re#e$
5e3lates "#rep -v"8
awk '&!re#e$!'
2 print t"e line iediately :efore a re#e$<
:3t not t"e line
2 1ontainin# t"e re#e$
awk '!re#e$!{print $};{$=$4}'
awk '!re#e$!{print 5$=="" ; "at1" on line
1" 0 $8};{$=$4}'
2 print t"e line iediately after a re#e$<
:3t not t"e line
2 1ontainin# t"e re#e$
awk '!re#e$!{#etline;print}'
2 #rep for DDD and ))) and @@@ 5in any order8
awk '!DDD!; !)))!; !@@@!'
2 #rep for DDD and ))) and @@@ 5in t"at
order8
awk '!DDD.*))).*@@@!'
2 print only lines of TL 1"ara1ters or lon#er
awk 'len#t" > T='
2 print only lines of less t"an TL 1"ara1ters
awk 'len#t" 6 T='
2 print se1tion of file fro re#3lar
e$pression to end of file
awk '!re#e$!<4'
awk '!re#e$!<*A.'
2 print se1tion of file :ased on line
n3:ers 5lines U-12< in1l3sive8
awk'-C==U<-C==12'
2 print line n3:er L2
awk '-C==L2'
awk '-C==L2 {print;e$it}' 2 ore
effi1ient on lar#e files
2 print se1tion of file :etween two re#3lar
e$pressions 5in1l3sive8
awk '!,owa!<!Fontana!' 2 1ase
sensitive
/*G*@>,B* 9*G*>,A- A. @*C>D,- G,-*/0
2 delete DGG :lank lines fro a file 5sae
as "#rep '.' "8
awk -.
awk '!.!'
@C*9,>/ D-9 >ID-V/0
/pe1ial t"anks to Qeter /. >illier for
"elpin# e wit" t"e first release
of t"is .DW file.
.or additional synta$ instr31tions<
in1l3din# t"e way to apply editin#
1oands fro a disk file instead of t"e
1oand line< 1ons3lt0
"sed X awk< 2nd *dition<" :y 9ale 9o3#"erty
and Drnold Co::ins
A'Ceilly< 1%%M
"E-,? >e$t Qro1essin#<" :y 9ale 9o3#"erty
and >i A'Ceilly
Iayden )ooks< 1%UM
"*ffe1tive awk Qro#rain#< Yrd *dition." :y
Drnold Co::ins
A'Ceilly< 2441
>o f3lly e$ploit t"e power of awk< one 3st
3nderstand "re#3lar
e$pressions." .or detailed dis13ssion of
re#3lar e$pressions< see
"Fasterin# Ce#3lar *$pressions< 2d edition"
:y Zeffrey .riedl
5A'Ceilly< 24428.
>"e an3al 5"an"8 pa#es on Eni$ systes ay
:e "elpf3l 5try "an awk"<
"an nawk"< "an re#e$p"< or t"e se1tion on
re#3lar e$pressions in "an
ed"8< :3t an pa#es are notorio3sly
diffi13lt. >"ey are not written to
tea1" awk 3se or re#e$ps to first-tie
3sers< :3t as a referen1e te$t
for t"ose already a1S3ainted wit" t"ese
tools.
E/* A. 'Ht' ,- awk /@C,Q>/0 .or 1larity in
do13entation< we "ave 3sed
t"e e$pression 'Ht' to indi1ate a ta:
1"ara1ter 54$4%8 in t"e s1ripts.
Dll versions of awk< even t"e E-,? /yste M
version s"o3ld re1o#niPe
t"e 'Ht' a::reviation.
2---end of file---
$W% &ne'(iners
$lthough awk can )e used to write #rograms of some com#le*ity, many
useful #rograms are not com#licated. +ere is a collection of short #rograms
that you might find handy and,or instructive:
1. -rint the total numn)er of in#ut lines:
*-9 { print -C }
2. -rint the tenth in#ut line:
-C == 14
!. -rint the last field of every in#ut line:
{ print $-. }
.. -rint the last field of the last in#ut line:
{ field = $-.}
*-9 { print field }
/. -rint every in#ut line with more than . fields:
-. > =
0. -rint every in#ut line in which the last field is more than .:
$-. > =
1. -rint the total num)er of fields in all in#ut lines:
{ nf = nf 7 -. }
*-9 { print nf }
2. -rint the total num)er of lines that contain )et" :
!)et"! { nlines = nlines 7 1 }
*-9 { print nlines }
3. -rint the largest first fields and the line that contains it ( assumes
some $1 is #ositive):
$1 > a$ { a$ = $1 ; a$lines = $4 }
*-9 { print a$< a$line8

14. -rint every line that has at least one field:
-. > 4
11. -ritn every line longer than 24 characters:
len#t"5$48 > U4
12. -rint the numer of fields in every line, followed )y the line itself:
{ print -.< $4 }
1!. -rint the first two fields, in o##osite order, of every line:
{ print $2< $1 }
1.. 5*change the first two fields of every line and then #rint the line:
{ tep = $1 ; $1 = $2 ; $2 = tep ; print }
1/. -rint every line witg rge first field re#laced )y the line num)er:
{ $1 = -C ; print }
10. -rint every line after erasing the second field:
{ $2 = ""; print }
11. -rint in reverse order the fields of every line:
{ for 5i=-. ; i>4 ; i=i-18 printf5 "Ns "< $18
printf5"Hn"8
}
12. -rint the sums of the fields of every line:
{ s3 = 4
for 5 i=1 ; i6=-. ; i=i718 s3 = s3 7 $i
print s3
}
13. $d u# all fields in all lines and #rint the sum:
{ for 5 i=1 ; i6=-. ; i=i71 8 s3 = s3 7 $i}
*-9 { print s3 }

24. -rint every line after re#lacing each field )y its a)solute value:
{ for 5i=1 ; i6=-. ; i=i718 if 5$i648 $i=-$i
print
}
6ource: The AWK Programming Language
&ne'liners
7seful 8&ne'liners8
9999999999999999999
7seful :awk; #rograms are often short, <ust a line or two. +ere is a
collection of useful, short #rograms to get you started. 6ome of these
#rograms contain constructs that haven;t )een covered yet. "he
descri#tion of the #rogram will give you a good idea of what is going
on, )ut #lease read the rest of the manual to )ecome an :awk; e*#ert
6ince you are reading this in =nfo, each line of the e*am#le code is
enclosed in >uotes, to re#resent te*t that you would ty#e literally.
"he e*am#les themselves re#resent shell commands that use single >uotes
to kee# the shell from inter#reting the contents of the #rogram. When
reading the e*am#les, focus on the te*t )etween the o#en and close
>uotes.
:awk ;? if (@A B ma*) ma* C @A D;
: 5@E ? #rint ma* D;;
"his #rogram #rints the ma*imum num)er of fields on any in#ut line.
:awk ;length(F4) B 24;;
"his #rogram #rints every line longer than 24 characters. "he sole
rule has a relational e*#ression as its #attern, and has no action
(so the default action, #rinting the record, is used).
:awk ;@A B 4;;
"his #rogram #rints every line that has at least one field. "his
is an easy way to delete )lank lines from a file (or rather, to
create a new file similar to the old file )ut from which the )lank
lines have )een deleted).
:awk ;? if (@A B 4) #rint D;;
"his #rogram also #rints every line that has at least one field.
+ere we allow the rule to match every line, then decide in the
action whether to #rint.
:awk ;B5G=@ ? for (i C 1H i IC 1H iJJ);
: #rint int(141 9 rand()) D;;
"his #rogram #rints 1 random num)ers from 4 to 144, inclusive.
:ls 'l A=(56 K awk ;? * JC F. D H 5@E ? #rint 8total )ytes: 8 * D;;
"his #rogram #rints the total num)er of )ytes used )y A=(56.
:e*#and A=(5 K awk ;? if (* I length()) * C length() D;
: 5@E ? #rint 8ma*imum line length is 8 * D;;
"his #rogram #rints the ma*imum line length of A=(5. "he in#ut is
#i#ed through the :e*#and; #rogram to change ta)s into s#aces, so
the widths com#ared are actually the right'margin columns.
:awk ;B5G=@ ? A6 C 8:8 D;
: ? #rint F1 K 8sort8 D; ,etc,#asswd;
"his #rogram #rints a sorted list of the login names of all users.
:awk ;? nlinesJJ D;
: 5@E ? #rint nlines D;;
"his #rograms counts lines in a file.
:awk ;5@E ? #rint @L D;;
"his #rogram also counts lines in a file, )ut lets :awk; do the
work.
:awk ;? #rint @L, F4 D;;
"his #rogram adds line num)ers to all its in#ut files, similar to
:cat 'n;.

"he G$W% Manual ' 7seful &ne'liners
7seful awk #rograms are often short, <ust a line or two. +ere is a collection
of useful, short #rograms to get you started. 6ome of these #rograms
contain constructs that haven;t )een covered yet. "he descri#tion of the
#rogram will give you a good idea of what is going on, )ut #lease read the
rest of the manual to )ecome an awk e*#ert
awk '{ if 5-. > a$8 a$ = -. }
*-9 { print a$ }'
"his #rogram #rints the ma*imum num)er of fields on any in#ut
line.
awk 'len#t"5$48 > U4'
"his #rogram #rints every line longer than 24 characters. "he
sole rule has a relational e*#ression as its #attern , and has no
action (so the default action , #rinting the record, is used).
awk '-. > 4'
"his #rogram #rints every line that has at least one field . "his is
an easy way to delete )lank lines from a file (or rather, to create
a new file similar to the old file )ut from which the )lank lines
have )een deleted).
awk '{ if 5-. > 48 print }'
"his #rogram also #rints every line that has at least one field .
+ere we allow the rule to match every line, then decide in the
action whether to #rint.
awk ')*+,- { for 5i = 1; i 6= M; i778
print int5141 * rand588 }'
"his #rogram #rints 1 random num)ers from 4 to 144, inclusive.
ls -l files | awk '{ $ 7= $= } ; *-9 { print "total :ytes0 "
$ }'
"his #rogram #rints the total num)er of )ytes used )y files .
e$pand file | awk '{ if 5$ 6 len#t"588 $ = len#t"58 }
*-9 { print "a$i3 line len#t" is " $ }'
"his #rogram #rints the ma*imum line length of file . "he in#ut is
#i#ed through the e$pand #rogram to change ta)s into s#aces,
so the widths com#ared are actually the right'margin columns.
awk ')*+,- { ./ = "0" }
{ print $1 | "sort" }' !et1!passwd
"his #rogram #rints a sorted list of the login names of all users.
awk '{ nlines77 }
*-9 { print nlines }'
"his #rograms counts lines in a file.
awk '*-9 { print -C }'
"his #rogram also counts lines in a file, )ut lets awk do the
work.
awk '{ print -C< $4 }'
"his #rogram adds line num)ers to all its in#ut files, similar to [1at
-n'

You might also like