grep -A1 格式输出

grep -A1 格式输出

我有 10 个文件,其中包含多个 IP ping 测试结果

--- 10.90.101.224 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
Some Text here...
--- 10.90.101.225 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
Some Text here...
--- 10.90.101.226 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms

我使用 >grep -A1 'ping statistics' *.log并得到下面的输出

--- 10.90.101.224 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
--- 10.90.101.225 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
--- 10.90.101.226 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms

我想获得以下格式的输出

--- 10.90.101.224 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2002ms
--- 10.90.101.225 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2002ms
--- 10.90.101.226 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2002ms

答案1

使用paste

grep -A1 'ping statistics' *.log | paste - -

paste默认情况下会在行之间添加一个制表符,将其更改为带有paste -d ' ' - -.

答案2

使用xargs

grep -A1 'ping statistics' *.log | xargs -n 15

-n max-args 每个命令行最多使用 max-args 个参数。

相关内容