原文地址:
http://www.funtoo.org/Awk_by_Example,_Part_1?ref=dzone
http://www.funtoo.org/Awk_by_Example,_Part_2
http://www.funtoo.org/Awk_by_Example,_Part_3
halt7 operator11 root0 shutdown6 sync5 bin1 ....etc.
username: halt uid:7 username: operator uid:11 username: root uid:0 username: shutdown uid:6 username: sync uid:5 username: bin uid:1 ....etc.
BEGIN { FS=":" } { print $1 }
#!/usr/bin/awk -f BEGIN { FS=":" } { print $1 }
/foo/ { print }
/[0-9]+\.[0-9]*/ { print }
$1 == "fred" { print $3 }
$5 ~ /root/ { print $3 }
{ if ( $5 ~ /root/ ) { print $3 } }
{ if ( $1 == "foo" ) { if ( $2 == "foo" ) { print "uno" } else { print "one" } } else if ($1 == "bar" ) { print "two" } else { print "three" } }
! /matchme/ { print $1 $3 $4 }
{ if ( $0!~ /matchme/ ) { print $1 $3 $4 } }
( $1 == "foo" ) && ( $2 == "bar" ) { print }
BEGIN { x=0 } /^$/ { x=x+1 } END { print "I found " x " blank lines.:)" }
x="1.01" # We just set x to contain the *string* "1.01" x=x+1 # We just added one to a *string* print x # Incidentally,these are comments:)
2.01
{ print ($1^2)+1 }
FS="\t+"
FS="[[:space:]]+"
FS="foo[0-9][0-9][0-9]"
NF == 3 { print "this particular record has three fields: " $0 }
{ if ( NF > 2 ) { print $1 " " $2 ":" $3 } }
(NR < 10 ) || (NR > 100) { print "We are on record number 1-9 or 101+" }{ #skip header if ( NR > 10 ) { print "ok,now for the real information!" } }
Jimmy the Weasel
100 Pleasant Drive
San Francisco,CA 12345