实例1:
[root@localhost ~]# awk 'BEGIN {print ARGV[0]}' /etc/fstab /etc/passwd #awk命令自身也包括 awk [root@localhost ~]# awk 'BEGIN {print ARGV[1]}' /etc/fstab /etc/passwd /etc/fstab [root@localhost ~]# awk 'BEGIN {print ARGV[2]}' /etc/fstab /etc/passwd /etc/passwd 十一、$NF - 最后字段内容$NF表示每行的最后一个字段的内容
实例1:
[root@localhost ~]# head -1 /etc/passwd root:x:0:0:young,geek,010110110,0101101101:/root:/bin/bash root@localhost ~]# head -1 /etc/passwd|awk -F: '{print $NF}' /bin/bash [root@localhost ~]# head -1 /etc/passwd|awk -F: '{print $(NF-1)}' /root [root@localhost ~]# head -1 /etc/passwd|awk -F: '{print $(NF-2)}' young,geek,010110110,0101101101 十二、IGNORECASE - 忽略大小写默认情况下, IGNORECASE 的值是 0,所有 awk 区分大小写。当把 IGNORECASE 的值设置为 1 时, awk 则不区分大小写,这在使用正则表达式和比较字符串时很有效率。
实例1:
[root@localhost ~]# cat items.txt
101,HD Camcorder,Video,210,10
102,Refrigerator,Appliance,850,2
103,MP3 Player,Audio,270,15
104,Tennis Racket,Sports,190,20
105,Laser Printer,Office,475,5
[root@localhost ~]# cat ign.awk
BEGIN {
FS=",";
IGNORECASE=1;
} {
if ($3 == "video") print $0;
if ($2 ~ "TENNIS") print $0;
}
[root@localhost ~]# awk -f ign.awk items.txt
101,HD Camcorder,Video,210,10
104,Tennis Racket,Sports,190,20
[root@localhost ~]# awk 'BEGIN{IGNORECASE=1} /video/{print}' items.txt
101,HD Camcorder,Video,210,10