问题描述
我在寻找一种解析多行命令输出的方法;
$ cat文件
ABC
123
62p4-123
DEF
456
62p4-456
我需要在每个第三行分组的o / p,以及3的倍数,即第3,第9个,后面是逗号分隔格式的第1,第4和第7和第2,第5和第8个
ABC,DEF
123,456
62p4-123,62p4-456
这只是一个示例格式,实际用例是 json o / p,我想使用 bash 中的工具/ t在 jq 中需要一个格式化选项我用于解析数据。
$ c> awk 喜欢 awk'NR%3 == 0',但我不能让其他行重复。
编辑: - 更新我的实际JSON o / p从 jq 返回以获得最有效的解决方案
4496
http:// xxx / yyy
/ home / build / branches / mmm / file1
4497
http:// xxx / yyy / zzz
/ home / build / branches / mmm / file1
4498
http:// xxx / yyy / build / branches / mmm / otherfile.c
预期o / p
4496,4497,4498
http:// xxx / yyy,http:// xxx / yyy / zzz,http:// xxx / yyy / zzz
/home/build/branches/mmm/file1,/home/build/branches/mmm/file1,home/build/branches/mmm/otherfile.c
解决方案以下是更简单的方法
$ pr -2ts,file
ABC,DEF
123,456
62p4-123,62p4-456
为其他输入将列数更改为3。
$ pr -3ts,file
4496,4497,4498
http:// xxx / yyy,http:// xxx / yyy / zzz,http:// xxx / yyy / zzz
/home/build/branches/mmm/file1,/home/build/branches/mmm/file1,/home/build/branches/mmm/otherfile.c
如果你只知道行数,而不是最终的列数,你可以这样做
$ pr - $(awk'END {print NR / 3}'file)-ts,file
Am looking for a way to parse a multi line command output; group every multiple of nth line and format in CSV format.
$ cat file ABC 123 62p4-123 DEF 456 62p4-456I need the o/p grouped at every 3rd line, and at multiples of 3, i.e 3rd, 6th and 9th followed by 1st,4th and 7th and 2nd, 5th and 8th respectively in comma separated format
ABC,DEF 123,456 62p4-123,62p4-456This is just a sample format and my actual use case is a json o/p that I want to format using tools/utilities in bash and I don't need a formatting option within jq I used for parsing the data.
I found several ways in awk like awk 'NR % 3 == 0' but I cannot make this repeat for other lines.
EDIT:- Am updating my actual JSON o/p returned from jq here to get the most effective solution
4496 http://xxx/yyy /home/build/branches/mmm/file1 4497 http://xxx/yyy/zzz /home/build/branches/mmm/file1 4498 http://xxx/yyy/zzz /home/build/branches/mmm/otherfile.cExpected o/p
4496,4497,4498 http://xxx/yyy,http://xxx/yyy/zzz,http://xxx/yyy/zzz /home/build/branches/mmm/file1,/home/build/branches/mmm/file1,home/build/branches/mmm/otherfile.c解决方案Here is a simpler way
$ pr -2ts, file ABC,DEF 123,456 62p4-123,62p4-456for the other input change the number of columns to 3.
$ pr -3ts, file 4496,4497,4498 http://xxx/yyy,http://xxx/yyy/zzz,http://xxx/yyy/zzz /home/build/branches/mmm/file1,/home/build/branches/mmm/file1,/home/build/branches/mmm/otherfile.cIf you only know the number of lines, not the final number of columns you can do this
$ pr -$(awk 'END{print NR/3}' file) -ts, file
这篇关于以CSV格式对每个第n行命令输出进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!