Linux awk
[root@localhost awk]# echo "aa bb l cc dd l ee ff" | awk -F 'l' '{print $1}' # 已 l 分割 第一个字符 aa bb
$0 表示当前整行内容,$1,$2 表示第一个字段,第二个字段
$NF就代表最后一个字段,$(NF-1)代表倒数第二个字段 [root@localhost zabbix_agentd.d]# echo "aa bb cc dd" | awk '{ print $NF}' dd [root@localhost zabbix_agentd.d]# echo "aa bb cc dd" | awk '{ print $(NF-1)}' cc
NR 表示当前行数
[root@localhost awk]# cat test.txt aa ss dd ff gg hh [root@localhost awk]# cat test.txt | awk '{print NR")", $0}' 1) aa ss 2) dd ff 3) gg hh
if 语句 [root@localhost awk]# echo "aa ss dd" | awk '{ if($3 == "dd") print $0; else print "nothing"}' aa ss dd [root@localhost awk]# echo "aa ss dds" | awk '{ if($3 == "dd") print $0; else print "nothing"}' nothing
本文来自博客园,作者:Python界-黄药师,转载请注明原文链接:https://www.cnblogs.com/niehaidong111/p/12613126.html