awk

2013年3月13日 星期三 晴sql




awkapache



1、簡單應用vim



一、顯示該行單詞數量bash



[root@desktop107 ~]# echo hello the world |awk '{print NF}'ssh


3ide



二、顯示該行結尾單詞ui



[root@desktop107 ~]# echo hello the world |awk '{print $NF}'ip


worldci



三、顯示該行所有內容文檔



[root@desktop107 ~]# echo hello the world |awk '{print $0}'


hello the world



四、顯示該行第一個單詞



[root@desktop107 ~]# echo hello the world |awk '{print $1}'


hello



五、顯示該行第一列第二列單詞



[root@desktop107 ~]# echo hello the world |awk '{print $1,$2}'


hello the


[root@desktop107 ~]#



2、搜索含有關鍵字「oo」的行



[root@desktop107 ~]# awk '/oo/' /etc/passwd


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


lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin


mail:x:8:12:mail:/var/spool/mail:/sbin/nologin


uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin


operator:x:11:0:operator:/root:/sbin/nologin


mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin


smmsp:x:51:51::/var/spool/mqueue:/sbin/nologin


[root@desktop107 ~]#



3、顯示第一列第三列信息



[root@desktop107 ~]# awk -F: '{print $1,$3}' /etc/passwd


root 0


bin 1


daemon 2


adm 3


lp 4


sync 5


shutdown 6


halt 7


mail 8


news 9


uucp 10


operator 11


games 12


gopher 13


ftp 14


nobody 99


nscd 28


vcsa 69


pcap 77


ntp 38


dbus 81


avahi 70


mailnull 47


smmsp 51


rpc 32


apache 48


cimsrvr 100


hsqldb 96


sshd 74


rpcuser 29


nfsnobody 65534


xfs 43


qemu 101


haldaemon 68


avahi-autoipd 102


gdm 42


quagga 92


chenglong 500


eric 501


[root@desktop107 ~]#



必須家分隔符,不然默認空格爲分隔符,對於文檔/etc/passwd將所有顯示



4、顯示含關鍵詞的特定行



[root@desktop107 ~]# awk -F: '/oo/{print $1,$3}' /etc/passwd


root 0


lp 4


mail 8


uucp 10


operator 11


mailnull 47


smmsp 51


[root@desktop107 ~]#



其全行內容爲



[root@desktop107 ~]# awk -F: '/oo/{print $0}' /etc/passwd


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


lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin


mail:x:8:12:mail:/var/spool/mail:/sbin/nologin


uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin


operator:x:11:0:operator:/root:/sbin/nologin


mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin


smmsp:x:51:51::/var/spool/mqueue:/sbin/nologin


[root@desktop107 ~]#



5、改變輸出字段分隔符



一、列中不能用-符號



[root@desktop107 ~]# awk -F: 'BEGIN{OFS="##"}/^root/{print $1-$4}' /etc/passwd


0



二、前兩列



[root@desktop107 ~]# awk -F: 'BEGIN{OFS="##"}/^root/{print $1,$2}' /etc/passwd


root##x



三、列中不能用~符號



[root@desktop107 ~]# awk -F: 'BEGIN{OFS="##"}/^root/{print $1~$4}' /etc/passwd


0



四、第1、3、四列



[root@desktop107 ~]# awk -F: 'BEGIN{OFS="##"}/^root/{print $1,$3,$4}' /etc/passwd


root##0##0



五、第1、3、5、六列



[root@desktop107 ~]# awk -F: 'BEGIN{OFS="##"}/^root/{print $1,$3,$5,$6}' /etc/passwd


root##0##root##/root


[root@desktop107 ~]#



6、自行擴展



一、顯示每行單詞數量並在每行後插入關鍵詞



[root@desktop107 ~]# awk -F: '{print $1,NR}{print "hello"}' /etc/passwd


root 1


hello


bin 2


hello


daemon 3


hello


adm 4


hello


lp 5


hello


sync 6


hello


shutdown 7


hello


halt 8


hello


mail 9


hello


news 10


hello


uucp 11


hello


operator 12


hello


games 13


hello


gopher 14


hello


ftp 15


hello


nobody 16


hello


nscd 17


hello


vcsa 18


hello


pcap 19


hello


ntp 20


hello


dbus 21


hello


avahi 22


hello


mailnull 23


hello


smmsp 24


hello


rpc 25


hello


apache 26


hello


cimsrvr 27


hello


hsqldb 28


hello


sshd 29


hello


rpcuser 30


hello


nfsnobody 31


hello


xfs 32


hello


qemu 33


hello


haldaemon 34


hello


avahi-autoipd 35


hello


gdm 36


hello


quagga 37


hello


chenglong 38


hello


eric 39


hello


[root@desktop107 ~]#



二、顯示所在行並在該行後加入關鍵詞



[root@desktop107 ~]# awk -F: '{print $1,NR,"hello"}' /etc/passwd


root 1 hello


bin 2 hello


daemon 3 hello


adm 4 hello


lp 5 hello


sync 6 hello


shutdown 7 hello


halt 8 hello


mail 9 hello


news 10 hello


uucp 11 hello


operator 12 hello


games 13 hello


gopher 14 hello


ftp 15 hello


nobody 16 hello


nscd 17 hello


vcsa 18 hello


pcap 19 hello


ntp 20 hello


dbus 21 hello


avahi 22 hello


mailnull 23 hello


smmsp 24 hello


rpc 25 hello


apache 26 hello


cimsrvr 27 hello


hsqldb 28 hello


sshd 29 hello


rpcuser 30 hello


nfsnobody 31 hello


xfs 32 hello


qemu 33 hello


haldaemon 34 hello


avahi-autoipd 35 hello


gdm 36 hello


quagga 37 hello


chenglong 38 hello


eric 39 hello


[root@desktop107 ~]#



awk表達式



一、加法運算



[root@desktop107 ~]# echo "test" |awk 'x=2 {print x+4}'


6



先顯示文檔再執行加法運算



二、加法、乘法運算



[root@desktop107 ~]# echo "test" |awk 'x=2,y=3 {print x+4,y*4}'


6 12


[root@desktop107 ~]#



三、列空白行



編輯一個文本



[root@desktop107 ~]# vim abc.txt


  1 ngjing


  2


  3 jinttiandnjdsjs


  4


  5


  6 haomeidetianqoi


  7


  8


  9


 10


 11 haoqonn


 12


 13 oa


 14


 15 aoqi miandui wan chaonglang


 16


 17


 18 jjjbghjkjbjh


~


                                                                               


[root@desktop107 ~]# awk '/^$/{print x+=1}' abc.txt


1


2


3


4


5


6


7


8


9


10


11



四、統計空白行數量



[root@desktop107 ~]# awk '/^$/{x+=1} END{print x}' abc.txt


11



五、匹配相關內容



[root@desktop107 ~]# awk -F: '$1~/root/{print $1}' /etc/passwd


root



匹配第一列中有關鍵詞root時打印第一列



[root@desktop107 ~]# awk -F: '$1~/root/{print $3}' /etc/passwd


0



匹配第一列中有關鍵詞root時打印第三列



[root@desktop107 ~]# awk -F: '$1~/root/{print $4}' /etc/passwd


0



匹配第一列中有關鍵詞root時打印第四列



[root@desktop107 ~]# awk -F: '$1~/root/{print $5}' /etc/passwd


root



匹配第一列中有關鍵詞root時打印第五列



六、知足特定條件時



[root@desktop107 ~]# awk -F: '$3>500{print $1}' /etc/passwd


nfsnobody


eric



顯示UID號大於500的賬戶                     

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息