天天看點

awk

[root@VM_166_163_centos ~]# echo "100ms 90ms" | awk '{print $NF}'

90ms

[root@VM_166_163_centos ~]# echo "100ms 90ms" | awk '{print +$NF}'

90

[root@VM_166_163_centos ~]# echo a b c d | awk 'BEGIN{one=1; two=2} {print $(one + two)}'

c

[root@VM_166_163_centos ~]# awk 'BEGIN {printf("|%*.*f|\n", 20,10,"123.456")}'

|      123.4560000000|

[root@VM_166_163_centos ~]# awk 'BEGIN {printf("|%*.*f|\n", -20,10,"123.456")}'

|123.4560000000      |

[root@VM_166_163_centos ~]# awk -v a=zhao -v b=cancan 'BEGIN{OFS="\t";print a,b}'      

zhao    cancan

[root@VM_166_163_centos ~]# awk -v a=zhao -v b=cancan 'BEGIN{OFS="\t";print a b}'

zhaocancan

[root@VM_166_163_centos ~]# awk -v a=zhao -v b=`pwd` 'BEGIN{OFS="\t";print a b}'  

zhao/root

[root@VM_166_163_centos ~]# awk  '{print a b}' a=zhao b=cancan 1

[root@VM_166_163_centos ~]# awk -v a=zhao -v b=cancan '{print a b}' 1

[root@VM_166_163_centos ~]# awk 'BEGIN {print n;print n}

{

if (n == 1) print "reading the first file"

if ( n == 2) print "reading the second file"

}' n=1 1 n=2 2

reading the first file

reading the second file

[root@VM_166_163_centos ~]# awk -v a=zhao 'BEGIN{print a}'

zhao                              -v參數定義的變量可以在begin裡面通路

[root@S997235 ~]# awk 'BEGIN {     

x = 1       

while (x<=4)

{   

print x

++x

}

}'

1

2

3

4

do {        

++x         

} while (x <= 4)

}' 

5

[root@VM_166_163_centos ~]# awk 'BEGIN{data[1.23] = '3.21';CONVFMT = "%d"

printf "<%s>\n", data[1.23]}'

<>

[root@VM_166_163_centos ~]# awk 'BEGIN{data[1.23] = '3.21';              

<3.21>

本文轉自 freeterman 51CTO部落格,原文連結:http://blog.51cto.com/myunix/1692975,如需轉載請自行聯系原作者