使用ps查看進程,做爲awk測試文件
# cat pslog
rpc 2829 1 0 10:01 ? 00:00:00 portmap
rpcuser 2849 1 0 10:01 ? 00:00:00 rpc.statd
root 2879 1 0 10:01 ? 00:00:00 rpc.idmapd
root 2968 1 0 10:01 ? 00:00:00 /usr/sbin/acpid
root 3031 1 0 10:01 ? 00:00:00 /usr/sbin/sshd
root 3046 1 0 10:01 ? 00:00:00 xinetd -stayalive -pidfile /var/run/xinetd.pid
root 3065 1 0 10:01 ? 00:00:01 sendmail: accepting connections
root 3085 1 0 10:01 ? 00:00:02 gpm -m /dev/input/mice -t imps2
htt 3114 1 0 10:01 ? 00:00:00 /usr/sbin/htt -retryonerror 0
htt 3115 3114 0 10:01 ? 00:00:00 htt_server -nodaemon
root 3125 1 0 10:01 ? 00:00:00 crond
顯示全部帶rpc字符串的全部行
# awk '/rpc/' pslog
rpc 2829 1 0 10:01 ? 00:00:00 portmap
rpcuser 2849 1 0 10:01 ? 00:00:00 rpc.statd
root 2879 1 0 10:01 ? 00:00:00 rpc.idmapd
顯示rpc開頭的第1,第8列
# awk '/rpc/{print $1,$8}' pslog
rpc portmap
rpcuser rpc.statd
root rpc.idmapd
顯示第一列中包含'u'的行
# awk '$1 ~ /u/' pslog
rpcuser 2849 1 0 10:01 ? 00:00:00 rpc.statd
顯示第一列的第一個字符是'h'的行
# awk '$1 ~ /^h/' pslog
htt 3114 1 0 10:01 ? 00:00:00 /usr/sbin/htt -retryonerror 0
htt 3115 3114 0 10:01 ? 00:00:00 htt_server -nodaemon
第二列開頭是1或2,並打印第2,8列在中間加入' : '分隔
# awk '$2 ~ /^[12]/{print $2,":"$8}' pslog
2829 :portmap
2849 :rpc.statd
2879 :rpc.idmapd
2968 :/usr/sbin/acpid
顯示第二列以數字8結尾的行
# awk '$2 ~ /8$/ {print $2,":"$8}' pslog
匹配第二列要大於3100的數
# awk '$2 > 3100 {print $2,$8}' pslog
3114 /usr/sbin/htt
3115 htt_server
3125 crond
匹配第二列大於3000並小於3100的數
# awk '$2 > 3000 && $2 < 3100 {print $2,$8}' pslog
3031 /usr/sbin/sshd
3046 xinetd
3065 sendmail:
3085 gpm
顯示第8列從rpc~sendmail的全部行
# awk '$8 ~ /rpc/,/sendmail/' pslog
rpcuser 2849 1 0 10:01 ? 00:00:00 rpc.statd
root 2879 1 0 10:01 ? 00:00:00 rpc.idmapd
root 2968 1 0 10:01 ? 00:00:00 /usr/sbin/acpid
root 3031 1 0 10:01 ? 00:00:00 /usr/sbin/sshd
root 3046 1 0 10:01 ? 00:00:00 xinetd -stayalive -pidfile /var/run/xinetd.pid
root 3065 1 0 10:01 ? 00:00:01 sendmail: accepting connections
length函數
他會顯示當前文本行中的字符個數,包含字段分隔符
# awk '{print length,$1,$8}' pslog |sort -n
53 root crond
55 rpc portmap
57 rpcuser rpc.statd
58 root rpc.idmapd
62 root /usr/sbin/sshd
63 root /usr/sbin/acpid
68 htt htt_server
77 htt /usr/sbin/htt
79 root gpm
使用'$0' 即包含一行中全部的內容
# awk '{print length,$0}' pslog |sort -n
53 root 3125 1 0 10:01 ? 00:00:00 crond
55 rpc 2829 1 0 10:01 ? 00:00:00 portmap
57 rpcuser 2849 1 0 10:01 ? 00:00:00 rpc.statd
58 root 2879 1 0 10:01 ? 00:00:00 rpc.idmapd
62 root 3031 1 0 10:01 ? 00:00:00 /usr/sbin/sshd
63 root 2968 1 0 10:01 ? 00:00:00 /usr/sbin/acpid
68 htt 3115 3114 0 10:01 ? 00:00:00 htt_server -nodaemon
77 htt 3114 1 0 10:01 ? 00:00:00 /usr/sbin/htt -retryonerror 0
79 root 3065 1 0 10:01 ? 00:00:01 sendmail: accepting connections
NR行號
顯示前5行的內容
# awk 'NR <5 {print length,$0}' pslog |sort -n
55 rpc 2829 1 0 10:01 ? 00:00:00 portmap
57 rpcuser 2849 1 0 10:01 ? 00:00:00 rpc.statd
58 root 2879 1 0 10:01 ? 00:00:00 rpc.idmapd
63 root 2968 1 0 10:01 ? 00:00:00 /usr/sbin/acpid
顯示第2行到第5行
# awk 'NR==2,NR==5 {print $1,$8}' pslog
rpcuser rpc.statd
root rpc.idmapd
root /usr/sbin/acpid
root /usr/sbin/sshd
以其餘符號分隔
以":"分隔字段文件,並以","輸出成他的第1,5列
# cat /etc/passwd |awk -F ":" '{OFS=",";print $1,$5}' |more
root,root
bin,bin
daemon,daemon
adm,adm
lp,lp
sync,sync