列出 UID > 499 的帳號及 UID
$ awk ‘BEGIN {FS=”:”}; $3 > 499 {print $1″t”$3}’ /etc/passwd
core 500
systemd-coredump 998
去除標題
$ ps a | awk ‘NR!=1 {print $0}’ | head
1237 tty1 Ss 0:00 /bin/login —
1238 ttyS0 Ss+ 0:06 /sbin/agetty –keep-baud 115200 38400 9600 ttyS0 vt220
1294 tty1 S+ 0:00 -bash
14202 pts/0 Ss 0:00 -bash
14264 pts/0 R+ 0:00 ps a
14265 pts/0 S+ 0:00 awk NR!=1 {print $0}
14266 pts/0 S+ 0:00 head[@more@]
列出第 2 行到第 5 行
$ ps a | awk ‘(NR>1 && NR<=5){print $0}’ | head
1237 tty1 Ss 0:00 /bin/login —
1238 ttyS0 Ss+ 0:06 /sbin/agetty –keep-baud 115200 38400 9600 ttyS0 vt220
1294 tty1 S+ 0:00 -bash
14202 pts/0 Ss 0:00 -bash
列出某一使用者的 UID
$ awk -F “:” ‘/docker/ {print $3}’ /etc/passwd
996
區分大小寫
$ awk -F”:” ‘BEGIN{IGNORECASE=1}{if($1==”core”){print $3}}’ /etc/passwd
500
計算行數
$ awk ‘END{print NR}’ /etc/profile
59
$ wc -l /etc/profile
59 /etc/profile
$ sed -n ‘$=’ /etc/profile
59
$ grep -c “” /etc/profile
59
計算空白行數
# cat blankline.awk
#!/usr/bin/awk
/^$/ {
x += 1
}
END {
print x;
}
# awk -f blankline.awk /etc/profile
11