Linux 如何从两个文本文件中交错行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4011814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to interleave lines from two text files
提问by Frank
What's the easiest/quickest way to interleave the lines of two (or more) text files? Example:
交错两个(或更多)文本文件的行的最简单/最快捷的方法是什么?例子:
File 1:
文件 1:
line1.1
line1.2
line1.3
File 2:
文件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?
当然,编写一个小的 Perl 脚本来打开它们并完成任务是很容易的。但我想知道是否有可能使用更少的代码,也许是使用 Unix 工具的单行代码?
采纳答案by codaddict
paste -d '\n' file1 file2
回答by Sujoy
cat file1 file2 |sort -t. -k 2.1
Here its specified that the separater is "." and that we are sorting on the first character of the second field.
这里指定分隔符为“。” 并且我们正在对第二个字段的第一个字符进行排序。
回答by Dwedit
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.
这是一种 GUI 方法:将它们粘贴到电子表格中的两列中,复制所有单元格,然后使用正则表达式将制表符替换为换行符。
回答by samgak
Here's a solution using awk
:
这是使用的解决方案awk
:
awk '{print; if(getline < "file2") print}' file1
produces this output:
产生这个输出:
line 1 from file1
line 1 from file2
line 2 from file1
line 2 from file2
...etc
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:
awk
如果您想向输出添加一些额外的格式,使用会很有用,例如,如果您想根据它来自哪个文件来标记每一行:
awk '{print "1: "1: line 1 from file1
2: line 1 from file2
1: line 2 from file1
2: line 2 from file2
...etc
; if(getline < "file2") print "2: "awk '{print; if(getline < "file2") print; else print ""}' file1
}' file1
produces this output:
产生这个输出:
awk '{print "1: "(cat -n file1 ; cat -n file2 ) | sort -n | cut -f2-
; if(getline < "file2") print "2: "(cat -n <(command1...) | sed 's/^/1\t/' ; cat -n <(command2...) | sed 's/^/2\t/' ; cat -n <(command3) | sed 's/^/3\t/' ) \
| sort -n | cut -f2- | sort -n | cut -f2-
; else print"2: "}' file1
Note: this code assumes that file1 is of greater than or equal length to file2.
注意:此代码假设 file1 的长度大于或等于 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:
如果 file1 包含的行数比 file2 多,并且您希望在 file2 完成后为 file2 输出空行,请在 getline 测试中添加一个 else 子句:
##代码##or
或者
##代码##回答by Joshua Goldberg
@Sujoy's answerpoints in a useful direction. You can add line numbers, sort, and strip the line numbers:
@Sujoy 的回答指出了一个有用的方向。您可以添加行号、排序和去除行号:
##代码##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:
请注意(我感兴趣),如果您使用的命令输出可能比其他命令运行得慢或快,则需要做更多的工作才能正确排序。在这种情况下,除了行号之外,您还需要添加/排序/删除另一个标签:
##代码##