Awk by Example--转载
原文地址: 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
BEGIN { FS="n" RS="" }
BEGIN { FS="n" RS="" } { print $1 "," $2 "," $3 }
Jimmy the Weasel,100 Pleasant Drive,San Francisco,CA 12345 Big Tony,200 Incognito Ave.,Suburbia,WA 67890
print "Hello","there","Jim!"
Hello there Jim!
BEGIN { FS="n" RS="" OFS="," } { print $1,$2,$3 }
Cousin Vinnie Vinnie's Auto Shop 300 City Alley Sosueme,OR 76543
BEGIN { FS="n" RS="" ORS="" }
Jimmy the Weasel 100 Pleasant Drive San Francisco,CA 12345 Big Tony 200 Incognito Ave. Suburbia,WA 67890 Cousin Vinnie Vinnie's Auto Shop 300 City Alley Sosueme,OR 76543
{ count=1 do { print "I get printed at least once no matter what" } while ( count?!= 1 ) }
for ( initial assignment; comparison; increment ) { code block }
for ( x = 1; x <= 4; x++ ) { print "iteration",x }
iteration 1 iteration 2 iteration 3 iteration 4
while (1) { print "forever and ever..." }
x=1 while(1) { print "iteration",x if ( x == 10 ) { break } x++ }
x=1 while (1) { if ( x == 4 ) { x++ continue } print "iteration",x if ( x > 20 ) { break } x++ }
for ( x=1; x<=21; x++ ) { if ( x == 4 ) { continue } print "iteration",x }
myarray[1]="jim" myarray[2]=456
for ( x in myarray ) { print myarray[x] }
jim 456
456 jim
a="1" b="2" c=a+b+3
myarr["1"]="Mr. Whipple" print myarr["1"]
myarr["1"]="Mr. Whipple" print myarr[1]
myarr["name"]="Mr. Whipple" print myarr["name"]
delete fooarray[1]
if ( 1 in fooarray ) { print "Ayep! It's there." } else { print "Nope! Can't find it." }
#!/usr/bin/awk -f BEGIN { x=1 b="foo" printf("%s got a?%d on the last testn","Jim",83) myout=sprintf("%s-%d",b,x) print myout }
Jim got a 83 on the last test foo-1
mystring="How are you doing today?" print mystring[3]
awk: string.gawk:59: fatal: attempt to use scalar as array
print length(mystring)
24
print index(mystring,"you")
9
print tolower(mystring) print toupper(mystring) print mystring
how are you doing today? HOW ARE YOU DOING TODAY? How are you doing today?
mysub=substr(mystring,startpos,maxlen)
print substr(mystring,9,3)
you
print match(mystring,/you/),RSTART,RLENGTH
9 9 3
sub(regexp,replstring,mystring)
sub(/o/,"O",mystring) print mystring mystring="How are you doing today?" gsub(/o/,mystring) print mystring
HOw are you doing today? HOw are yOu dOing tOday?
numelements=split("Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec",mymonths,",")
print mymonths[1],mymonths[numelements]
Jan Dec
{ print length() }
23 Aug 2000 food - - Y Jimmy's Buffet 30.25
23 Aug 2000 - inco - Y Boss Man 2001.00
#!/usr/bin/awk -f BEGIN { FS="t+" months="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec" }
print monthdigit("Mar")
3
function doincome(mybalance) { mybalance[curmonth,$3] += amount mybalance[0,$3] += amount }
{ curmonth=monthdigit(substr($1,4,3)) amount=$7
END { bal=0 bal2=0 for (x in globcat) { bal=bal+balance[0,x] bal2=bal2+balance2[0,x] } printf("Your available funds:?%10.2fn",bal) printf("Your account balance:?%10.2fn",bal2) }
Your available funds: 1174.22 Your account balance: 2399.33 ,put all the above code into a text file called??and do?,and then type?. The balance script will then add up all your transactions and print out a two-line balance summary for you.
<h3 style="box-sizing: border-box; font-family: 'Open Sans'; font-weight: 500; line-height: 1.1; margin-top: 20px; margin-bottom: 10px; font-size: 20px;"><span id="In_defense_of_awk" class="mw-headline" style="box-sizing: border-box;">In defense of awk (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |