shell脚本实现将文件中的几行输出合并成一行显示

shell脚本实现将文件中的几行输出合并成一行显示

现在有一个文件aa.txt ,其实现格式如下,而我想实现的结果是192.168.1.17 down

[root@localhost shell]# cat aa.txt
192.168.1.17
down
192.168.1.103
open
192.168.1.221
open
192.168.1.104
down
192.168.1.175
down
192.168.1.112
open
192.168.1.11
down

[root@localhost shell]# awk ‘{if (NR%2==0){print $0} else {printf”%s “,$0}}’ aa.txt
192.168.1.17 down
192.168.1.103 open
192.168.1.221 open
192.168.1.104 down
192.168.1.175 down
192.168.1.112 open
192.168.1.11 down

或者

[root@localhost shell]# cat aa.txt | awk ‘{if(NR%2!=0)ORS=” “;else ORS=”\n”;print}’
192.168.1.17 down
192.168.1.103 open
192.168.1.221 open
192.168.1.104 down
192.168.1.175 down
192.168.1.112 open
192.168.1.11 down

posted @ 2021-01-05 17:11  chengxuyonghu  阅读(829)  评论(0编辑  收藏  举报