How can I add a newline after match is found
In a substitution, &
in the replacement will be replaced with whatever matched. So:
sed 's/64/\n&/g' file
This sed
command should work with both gnu and BSD sed versions:
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
Update: Here is gnu awk command to do the same:
awk '1' RS='64' ORS='\n64' file
This might be more like what you really need (uses GNU awk for multi-char 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