加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

Awk by Example--转载

发布时间:2020-12-13 14:06:22 所属栏目: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:

原文地址:

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

Big Tony
200 Incognito Ave.
Suburbia,WA 67890

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="" 
} 

{
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 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"
}

function monthdigit(mymonth) {
return (index(months,mymonth)+3)/4
}

print monthdigit("Mar")

3

        
function doincome(mybalance) {
    mybalance[curmonth,$3] += amount
    mybalance[0,$3] += amount        
}

function doexpense(mybalance) {
mybalance[curmonth,$2] -= amount
mybalance[0,$2] -= amount
}

function dotransfer(mybalance) {
mybalance[0,$2] -= amount
mybalance[curmonth,$3] += amount
mybalance[curmonth,$3] += amount
}

{
    curmonth=monthdigit(substr($1,4,3))
    amount=$7
#record all the categories encountered
if ( $2?!= "-" )
    globcat[$2]="yes"
if ( $3?!= "-" )
    globcat[$3]="yes"

#tally up the transaction properly
if ( $2 == "-" ) {
    if ( $3 == "-" ) {
        print "Error: inc and exp fields are both blank!"
        exit 1
    } else {
        #this is income
        doincome(balance)
        if ( $5 == "Y" )
            doincome(balance2)
    }
} else if ( $3 == "-" ) {
    #this is an expense 
    doexpense(balance)
    if ( $5 == "Y" ) 
        doexpense(balance2)
} else {
    #this is a transfer
    dotransfer(balance)
    if ( $5 == "Y" ) 
        dotransfer(balance2)
}                        

}

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
<p style="box-sizing: border-box; margin-top: 0px; margin-right: 0px; margin-left: 0px; font-family: 'Open Sans'; line-height: 20px;">In this series of articles,and UNIX.
<li style="box-sizing: border-box;">
<a class="external text" style="box-sizing: border-box; color: #047ba2; text-decoration: none; background: transparent;" href="http://www.gnu.org/software/gawk/manual/gawk.html"&gt;The GNU Awk User's Guide?is available for online reference.
<li style="box-sizing: border-box;">
<a class="external text" style="box-sizing: border-box; color: #047ba2; text-decoration: none; background: transparent;" href="http://www.folkstalk.com/2011/12/good-examples-of-awk-command-in-unix.html"&gt;Awk Command?daily useful examples.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读