Awk by Example--转载
发布时间:2021-02-06 15:24:08 所属栏目:Linux 来源:网络整理
导读:原文地址: 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:
{
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" } |