[chengmo@centos5 shell]$ awk 'NR%2==1{next}{print NR,$0;}' text.txt 2 b 4 d 當記錄行號除以2余 1,就跳過當前行。下面的print NR,$0也不會執(zhí)行。 下一行開始,程序有開始判斷NR%2 值。這個時候記錄行號是:2 ,就會執(zhí)行下面語句塊:'print NR,$0'
awk next使用實例:
復制代碼
代碼如下:
要求: 文件:text.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
需要通過awk將輸出格式變成: 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
分析: 分析發(fā)現(xiàn)需要將包含有“web”行進行跳過,然后需要將內(nèi)容與下面行合并為一行。 [chengmo@centos5 shell]$ awk '/^web/{T=$0;next;}{print T":/t"$0;}' test.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