linux 中awk用法

awk 用法

awk '条件类型1 {动作1} 条件类型2 {动作2} ...' filename

awk内置变量

FS :输入域分隔符,默认为一个空格或tab

NR:目前 awk处理的是第几行数据

NF: 每一行拥有的字段数目

$0:代表当前行。 $1,$2,$3...代表被分隔后的各字段

例子: 打印$2,$3中间用" %% "分隔符

vigar@vigar-laptop:~$ ifconfig | grep "inet addr"

          inet addr:127.0.0.1  Mask:255.0.0.0

          inet addr:192.168.0.100  Bcast:192.168.0.255  Mask:255.255.255.0

vigar@vigar-laptop:~$ ifconfig | grep "inet addr" | awk '{print $2 " %% " $3}'

 addr:127.0.0.1 %% Mask:255.0.0.0

addr:192.168.0.100 %% Bcast:192.168.0.255

区别NR与NF的意义

vigar@vigar-laptop:~$ last -n 5                                                      

vigar    pts/0        :0.0             Tue Sep 18 23:36   still logged in   

vigar    tty7         :0               Tue Sep 18 23:23   still logged in   

reboot   system boot  2.6.32-21-generi Tue Sep 18 23:22 - 23:53  (00:31)    

vigar    pts/1        :0.0             Mon Sep 17 21:31 - down   (02:00)    

vigar    tty1                          Mon Sep 17 21:11 - down   (02:21)    



wtmp begins Sat Sep  1 15:35:42 2012

vigar@vigar-laptop:~$ last -n 5 | awk '{print $1 "\t lines: " NR  "\t columns: " NF}'

vigar     lines: 1     columns: 10

vigar     lines: 2     columns: 10

reboot     lines: 3     columns: 11

vigar     lines: 4     columns: 10

vigar     lines: 5     columns: 9

          lines: 6     columns: 0

wtmp      lines: 7     columns: 7

从/etc/passwd中找出第三列小于10的数据

vigar@vigar-laptop:~$ cat /etc/passwd | awk '{FS=":"} $3<10 {print $1 "\t" $3}'

root:x:0:0:root:/root:/bin/bash    

daemon    1

bin       2

sys       3

sync      4

games     5

man       6

lp        7

mail      8

news      9

其中有问题,第一行的数据未正常显示,因为第一行时FS变量尚未被正常设置,这时要用begin

vigar@vigar-laptop:~$ cat /etc/passwd | awk 'BEGIN {FS=":"} $3<10 {print $1 "\t" $3}'

root    0

daemon    1

bin    2

sys    3

sync    4

games    5

man    6

lp    7

mail    8

news    9

用一段awk脚本完成工资总数统计,呵呵,熟悉的情况下这比excel还快咧。。。

vigar@vigar-laptop:~$ cat pay.txt 

name 1st 2nd 3th

johnny 23 23 34

perry  24 24 25

sherman 33 33 35

vigar@vigar-laptop:~$ cat pay.txt | awk 'NR==1 {printf "%10s %10s %10s %10s %10s\n",$1,$2,$3,$4,"Total"} NR>=2 {total=$2+$3+$4; printf "%10s %10d %10d %10d %10.2f\n",$1,$2,$3,$4,total}'

      name        1st        2nd        3th      Total

    johnny         23         23         34      80.00

     perry         24         24         25      73.00

   sherman         33         33         35     101.00

vigar@vigar-laptop:~$ 

 

test-/opt/CC/hello/bin> ps -aux | awk '$4>0 {print $1 "\t"  $2 "\t" $4 "\t"  $11}'

shiadmin        7470    0.1     /usr/NX/bin/nxagent

p4admin 12230   15.2    java

p4admin 13995   0.2     java

shiadmin        17118   1.0     /usr/lib/jvm/jre-1.6.0-openjdk.x86_64/bin/java

shiadmin        21822   1.2     /usr/bin/java

p4admin 25350   0.5     java

p4admin 25965   0.3     java

p4admin 27456   1.2     java

p4m     28450   0.5     /opt/mqm/bin/amqzmuc0

p4m     28510   0.2     /opt/mqm/bin/amqzlaa0

p4m     29844   0.6     /opt/mqm/bin/amqrmppa

 

你可能感兴趣的:(linux)