Awk by Example–转载

原文地址: http://www.funtoo.org/Awk_by_Example,_Part_1?ref=dzone http://www.funtoo.org/Awk_by_Example,_P

原文地址:

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

作者: dawei

【声明】:永州站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

为您推荐

联系我们

联系我们

0577-28828765

在线咨询: QQ交谈

邮箱: xwei067@foxmail.com

工作时间:周一至周五,9:00-17:30,节假日休息

返回顶部