使用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