bash - 找到匹配项后如何添加换行符

标签 bash awk sed

我想在 ping 输出中使用 awk 或 sed 在数字 64 之后添加一个换行符,如下所示:

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 命令模式。

我尝试使用附加和插入模式..但没有正确使用它们

最佳答案

在替换中,替换中的 & 将被替换为匹配的任何内容。所以:

sed 's/64/\n&/g' file

关于bash - 找到匹配项后如何添加换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24707352/

相关文章:

linux - 将 shell 脚本变量设置为命令的输出

linux - 在linux中将tab转换为fasta格式

Linux bash : how to replace 2 words in a file from bash command line

sed 或 perl + 在两行之间插入文本

regex - 从日志文件中提取特定的 XML

bash - 在 bash 中使用数组更改行分隔文本文件

Bash shell 如何将日期格式化为小写月份?

bash - 如何退出最近的 getopts 选项?

bash - 如何在 bash 中成对减去字段?

bash - grep 特定字符串并用 sed 替换该文件