awk FS:输入字段分隔符 OFS:输出字段分隔符 RS:输入时的换行符 ORS:输出的换行符 NF:字段数量 $NF:最后一个字段 NR:统计行数 FNR:各个文件的行数统计 FILENAME:当前处理的文件名路径 ARGC:命令行参数的个数 ARGV:数组,以ARGV($1,...)调用 1.[root@localhost ~]# awk -v FS=":" '{print $1}' /etc/passwd [root@localhost ~]# awk '{print $1}' /etc/passwd [root@localhost ~]# gawk -v FS=":" '{print $1}' /etc/passwd相当于[root@localhost ~]# gawk -F: '{print $1}' /etc/passwd [root@localhost ~]# awk -v FS=":" '{print $1,$3,$7}' /etc/passwd [root@localhost ~]# awk -v FS=':' '{print $1,$3,$7}' /etc/passwd [root@localhost ~]# awk -v FS=':' -v OFS='#' '{print $1,$3,$7}' /etc/passwd [root@localhost ~]# awk -v RS=':' -v ORS='#' '{print}' /etc/passwd root#x#0#0#root#/root#/bin/bash bin#x#1#1#bin#/bin#/sbin/nologin daemon#x#2#2#daemon#/sbin#/sbin/nologin adm#x#3#4#adm#/var/adm#/sbin/nologin lp#x#4#7#lp#/var/spool/lpd#/sbin/nologin [root@localhost ~]# awk '{print NF}' /etc/fstab 统计行字段个数; [root@localhost ~]# awk '{print $NF}' /etc/fstab cat /etc/fstab 显示最后一个字段 [root@localhost ~]# awk 'BEGIN{print ARGC}' /etc/fstab /etc/issue 3 [root@localhost ~]# awk 'BEGIN{print ARGV[0]}' /etc/fstab /etc/issue awk [root@localhost ~]# awk -v test='glq hello' '{print test,$1}' /etc/fstab glq hello glq hello # glq hello # glq hello # [root@localhost ~]# awk 'BEGIN{test="welcome";print test,$2}' /etc/fstab welcome [root@localhost ~]# awk -v test='to my log' '{print test,$2}' /etc/fstab to my log to my log to my log /etc/fstab to my log Created to my log to my log Accessible [root@localhost ~]# awk -F: -v test="my log" '{print test,$7}' /etc/passwd my log /bin/bash my log /sbin/nologin my log /sbin/nologin ---------------------------- printf [root@localhost ~]# awk -F: '{printf "username: %-15s,UID: %d ",$1,$3}' /etc/passwd username: root ,UID: 0 username: bin ,UID: 1 username: daemon ,UID: 2 username: adm ,UID: 3 [root@localhost ~]# awk -F: '{printf "username: %-10s,UID: %-1d, Shell: %s ",$1,$3,$7}' /etc/passwd username: root ,UID: 0, Shell: /bin/bash username: bin ,UID: 1, Shell: /sbin/nologin username: daemon ,UID: 2, Shell: /sbin/nologin [root@localhost ~]# awk -F: '{printf "username: %-10s UID: %-1d Shell: %s ",$1,$3,$7}' /etc/passwd username: root UID: 0 Shell: /bin/bash username: bin UID: 1 Shell: /sbin/nologin username: daemon UID: 2 Shell: /sbin/nologin if判断语句: [root@localhost ~]# awk -F: '{$3>=1000?usertype="common user":usertype="system user";printf "%15s:%-s ",$1,usertype}' /etc/passwd root:system user bin:system user daemon:system user adm:system user --------------------------------------- [root@localhost ~]# awk '/^UUID/{print $1}' /etc/fstab UUID=63539c8b-2d17-4c5e-aa11-e232515bba61 [root@localhost ~]# awk '!/^UUID/{print $1}' /etc/fstab # # # # # # # /dev/mapper/centos-root /dev/mapper/centos-swap [root@localhost ~]# awk -F: '$3<1000{print "user:"$1,$3}' /etc/passwd user:root 0 user:bin 1 user:daemon 2 user:adm 3 user:lp 4 user:sync 5 [root@localhost ~]# awk -F: '$NF=="/bin/bash"{print $1,$NF}' /etc/passwd root /bin/bash testdb /bin/bash test1 /bin/bash [root@localhost ~]# awk -F: '$NF~/bash$/{print $1,$NF}' /etc/passwd root /bin/bash testdb /bin/bash test1 /bin/bash [root@localhost ~]# awk -F: '/^root/,/^ftp/{print $1}' /etc/passwd root bin daemon adm lp sync shutdown halt mail operator games ftp [root@localhost ~]# awk -F: '(NR>2&&NR<=10){print $1}' /etc/passwd daemon adm lp sync shutdown halt mail operator BEGIN和END使用: [root@localhost ~]# awk -F: 'BEGIN{print "username UID ------------------------"}{printf "%-21s %s ",$1,$3}' /etc/passwd username UID ------------------------ root 0 bin 1 daemon 2 adm 3 lp 4 sync 5 shutdown 6 halt 7 mail 8 operator 11 games 12 ftp 14 nobody 99 avahi-autoipd 170 systemd-bus-proxy 999 systemd-network 998 dbus 81 polkitd 997 tss 59 postfix 89 sshd 74 mysql 27 testdb 1000 test1 1001 [root@localhost ~]# awk -F: 'BEGIN{print "username UID ------------------------"}{printf "%-21s %s ",$1,$3}END{print "end ----------------------"}' /etc/passwd username UID ------------------------ root 0 bin 1 daemon 2 adm 3 lp 4 sync 5 shutdown 6 halt 7 mail 8 operator 11 games 12 ftp 14 nobody 99 avahi-autoipd 170 systemd-bus-proxy 999 systemd-network 998 dbus 81 polkitd 997 tss 59 postfix 89 sshd 74 mysql 27 testdb 1000 test1 1001 end ----------------------