联合“表”用 awk
我在一个文件中有多个“表”,例如:
col1, col2, col3, col4
1, 2, 3, 4
5, 6, 7, 8
col2, col3, col5
10, 11, 12
13, 14, 15
我想将这两个表折叠为:(
col1, col2, col3, col4, col5
1 , 2 , 3 , 4 ,
5 , 6 , 7 , 8 ,
, 10 , 11 , , 12
, 13 , 14 , , 15
注意:留下额外的空格只是为了让事情更容易理解)
这似乎需要至少 2 遍,一次到收集完整的列列表,并收集另一列以创建输出表。用 awk 可以做到这一点吗?如果没有,您会推荐什么其他工具?
I have multiple "tables" in a file, such as:
col1, col2, col3, col4
1, 2, 3, 4
5, 6, 7, 8
col2, col3, col5
10, 11, 12
13, 14, 15
And I would like to collapse these 2 tables to:
col1, col2, col3, col4, col5
1 , 2 , 3 , 4 ,
5 , 6 , 7 , 8 ,
, 10 , 11 , , 12
, 13 , 14 , , 15
(Note: extra whitespace left just to make things easier to understand)
This would seem to require at least 2 passes, one to collect the full list of columns, and another one to create the output table. Is it possible to do this with awk? If not, what other tool would you recommend?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试一下:
代码:
调用:
输出:
HTH Chris
give this a try:
Code:
Call with:
Output:
HTH Chris
该代码假定表由空行分隔:
如果表位于单独的文件中:
The code assumes that the tables are separated by empty lines:
If you have the tables in separate files:
这是一个一次性的 Perl 解决方案。它假设文件中每个表之间至少有一个空行。
输出
Here's a one-pass perl solution. It assumes there is at least one blank line between each table in the file.
output