我想在文件中搜索数字64后添加新行。我喜欢使用awk或sed来做这件事。
64 bytes from 170.198.42.128: icmp_seq=1 ttl=60 time=83.4 ms 64 bytes from
170.198.42.128: icmp_seq=2 ttl=60 time=76.6 ms 64 bytes from 170.198.42.128: icmp_seq=3
ttl=60 time=70.8 ms 64 bytes from 170.198.42.128: icmp_seq=4 ttl=60 time=76.6 ms ---
170.198.42.128 ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time
3000ms rtt min/avg/max/mdev = 70.861/76.924/83.493/4.473 ms
我使用此命令
cat file | sed 's/64/\n/g'
但这取代了数字64.我想要打破模式并显示以64开头正确的ping命令模式
我尝试使用追加和插入模式..但没有正确使用它们
需要帮助
答案 0 :(得分:6)
在替换中,替换中的&
将替换为匹配的任何内容。所以:
sed 's/64/\n&/g' file
答案 1 :(得分:3)
这个sed
命令应该适用于gnu和BSD sed版本:
sed $'s/64/\\\n&/g' file
64 bytes from 170.198.42.128: icmp_seq=1 ttl=60 time=83.4 ms
64 bytes from
170.198.42.128: icmp_seq=2 ttl=60 time=76.6 ms
64 bytes from 170.198.42.128: icmp_seq=3
ttl=60 time=70.8 ms
64 bytes from 170.198.42.128: icmp_seq=4 ttl=60 time=76.6 ms ---
170.198.42.128 ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time
3000ms rtt min/avg/max/mdev = 70.861/76.924/83.493/4.473 ms
更新:以下是gnu awk命令:
awk '1' RS='64' ORS='\n64' file
答案 2 :(得分:2)
这可能更像你真正需要的(使用GNU awk进行多字符RS):
$ gawk -v RS='^$' '{gsub(/[[:space:]]+/," "); gsub(/(\<[[:digit:]]+\> bytes|---)/,"\n&"); sub(/^\n/,"")}1' file
64 bytes from 170.198.42.128: icmp_seq=1 ttl=60 time=83.4 ms
64 bytes from 170.198.42.128: icmp_seq=2 ttl=60 time=76.6 ms
64 bytes from 170.198.42.128: icmp_seq=3 ttl=60 time=70.8 ms
64 bytes from 170.198.42.128: icmp_seq=4 ttl=60 time=76.6 ms
--- 170.198.42.128 ping statistics
--- 4 packets transmitted, 4 received, 0% packet loss, time 3000ms rtt min/avg/max/mdev = 70.861/76.924/83.493/4.473 ms