linux中awk命令中的next的用法
1、linux中awk中next命令相当于循环语句中的continue
[root@centos7 test2]# seq 6 1 2 3 4 5 6 [root@centos7 test2]# seq 6 | awk '$0 ~ /5/ {next}; {print $0}' ## 当前行匹配5的时候,跳过本次循环 1 2 3 4 6
2、测试数据
[root@centos7 test2]# ls a.txt [root@centos7 test2]# cat a.txt web01[192.168.2.100] httpd ok tomcat ok sendmail ok web02[192.168.2.101] httpd ok postfix ok web03[192.168.2.102] mysqld ok httpd ok
if实现数据折叠:
[root@centos7 test2]# ls a.txt [root@centos7 test2]# cat a.txt web01[192.168.2.100] httpd ok tomcat ok sendmail ok web02[192.168.2.101] httpd ok postfix ok web03[192.168.2.102] mysqld ok httpd ok [root@centos7 test2]# awk '{if($0 ~ /^web/){title = $0} else {print title, $0}}' a.txt web01[192.168.2.100] httpd ok web01[192.168.2.100] tomcat ok web01[192.168.2.100] sendmail ok web02[192.168.2.101] httpd ok web02[192.168.2.101] postfix ok web03[192.168.2.102] mysqld ok web03[192.168.2.102] httpd ok ## next的作用是跳过本次迭代 [root@centos7 test2]# awk '{if($0 ~ /^web/) {title = $0; next}{print title, $0}}' a.txt web01[192.168.2.100] httpd ok web01[192.168.2.100] tomcat ok web01[192.168.2.100] sendmail ok web02[192.168.2.101] httpd ok web02[192.168.2.101] postfix ok web03[192.168.2.102] mysqld ok web03[192.168.2.102] httpd ok