如何交错两个文本文件中的行
交错两个(或更多)文本文件的行的最简单/最快的方法是什么?示例:
文件 1:
line1.1
line1.2
line1.3
文件 2:
line2.1
line2.2
line2.3
交错:
line1.1
line2.1
line1.2
line2.2
line1.3
line2.3
当然,编写一个小 Perl 脚本来打开它们并执行任务是很容易的。但我想知道是否可以使用更少的代码(也许是使用 Unix 工具的单行代码)?
What's the easiest/quickest way to interleave the lines of two (or more) text files? Example:
File 1:
line1.1
line1.2
line1.3
File 2:
line2.1
line2.2
line2.3
Interleaved:
line1.1
line2.1
line1.2
line2.2
line1.3
line2.3
Sure it's easy to write a little Perl script that opens them both and does the task. But I was wondering if it's possible to get away with fewer code, maybe a one-liner using Unix tools?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是使用
awk
的解决方案:产生以下输出:
如果您想向输出添加一些额外的格式(例如,如果您想标记每一行),则使用
awk
会很有用基于它来自哪个文件:产生以下输出:
注意:此代码假定 file1 的长度大于或等于 file2。
如果 file1 包含的行数多于 file2,并且您希望在 file2 完成后输出空行,请在 getline 测试中添加 else 子句:
或
Here's a solution using
awk
:produces this output:
Using
awk
can be useful if you want to add some extra formatting to the output, for example if you want to label each line based on which file it comes from:produces this output:
Note: this code assumes that file1 is of greater than or equal length to file2.
If file1 contains more lines than file2 and you want to output blank lines for file2 after it finishes, add an else clause to the getline test:
or
@Sujoy 的回答指出了一个有用的方向。您可以添加行号、排序和删除行号:
请注意(我感兴趣)如果您使用的命令输出不是静态文件,而运行速度可能比静态文件慢或快,则需要做更多工作才能正确排序。彼此。在这种情况下,除了行号之外,您还需要添加/排序/删除另一个标签:
@Sujoy's answer points in a useful direction. You can add line numbers, sort, and strip the line numbers:
Note (of interest to me) this needs a little more work to get the ordering right if instead of static files you use the output of commands that may run slower or faster than one another. In that case you need to add/sort/remove another tag in addition to the line numbers:
使用 GNU sed:
输出:
With GNU sed:
Output:
这是一种 GUI 方法:将它们粘贴到电子表格的两列中,复制所有单元格,然后使用正则表达式将制表符替换为换行符。
Here's a GUI way to do it: Paste them into two columns in a spreadsheet, copy all cells out, then use regular expressions to replace tabs with newlines.
这里指定分隔符是“.”。我们正在对第二个字段的第一个字符进行排序。
Here its specified that the separater is "." and that we are sorting on the first character of the second field.