我有一个命令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
答案 0 :(得分:3)
使用GNU grep
和pr
$ 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
下的bash: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