水平粘贴grep输出

时间:2017-04-20 06:55:55

标签: bash grep paste

我有一个命令grep -A5 "Account:1\|Account:2\|Account:3" myfile

这给我的结果如下

Sometext1 Account:1
Detail 1
Detail 2
Detail 3
Detail 4
Detail 5
--
Sometext2 Account:2
Detail 1
Detail 2
Detail 3
Detail 4
Detail 5
--
Sometext3 Account:3
Detail 1
Detail 2
Detail 3
Detail 4
Detail 5

有没有办法水平粘贴此结果。我知道我可以通过首先编写3个文件,然后使用paste来寻找更好的解决方案。

预期输出

Sometext1 Account:1     Sometext2 Account:2     Sometext3 Account:3
Detail 1                Detail 1                Detail 1
Detail 2                Detail 2                Detail 2
Detail 3                Detail 3                Detail 3
Detail 4                Detail 4                Detail 4
Detail 5                Detail 5                Detail 5

2 个答案:

答案 0 :(得分:3)

使用GNU greppr

$ grep -A5 --no-group-separator 'Account:[123]' myfile | pr -3t
Sometext1 Account:1     Sometext2 Account:2     Sometext3 Account:3
Detail 1                Detail 1                Detail 1
Detail 2                Detail 2                Detail 2
Detail 3                Detail 3                Detail 3
Detail 4                Detail 4                Detail 4
Detail 5                Detail 5                Detail 5
  • --no-group-separator以避免结果之间的--
  • -3中的pr表示所需的列数

答案 1 :(得分:1)

paste下的

paste <(grep -A5 Account:1 myfile) <(grep -A5 Account:2 myfile) <(grep -A5 Account:3 myfile)
Sometext1 Account:1     Sometext2 Account:2     Sometext3 Account:3
Detail 1        Detail 1        Detail 1
Detail 2        Detail 2        Detail 2
Detail 3        Detail 3        Detail 3
Detail 4        Detail 4        Detail 4
Detail 5        Detail 5        Detail 5