设为首页 - 加入收藏 ASP站长网(Aspzz.Cn)- 科技、建站、经验、云计算、5G、大数据,站长网!
热搜: 创业者 数据 手机
当前位置: 首页 > 服务器 > 搭建环境 > Linux > 正文

Awk by Example--转载(3)

发布时间:2021-02-04 06:16 所属栏目:118 来源:网络整理
导读:{ x=1 while ( xNF ) { print $x "\t" x++ } print $NF "\n" } Jimmy the Weasel 100 Pleasant Drive San Francisco,CA 12345 Big Tony 200 Incognito Ave. Suburbia,WA 67890Cousin Vinnie Vinnie's Auto Shop 300

{
x=1
while ( x<NF ) {
print $x "\t"
x++
}
print $NF "\n"
}

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 test\n","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"
}

                        

(编辑:ASP站长网)

网友评论
推荐文章
    热点阅读