如何交错两个文本文件中的行

发布于 2024-09-29 01:47:53 字数 331 浏览 1 评论 0原文

交错两个(或更多)文本文件的行的最简单/最快的方法是什么?示例:

文件 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

哆啦不做梦 2024-10-06 01:47:53
paste -d '\n' file1 file2
paste -d '\n' file1 file2
南街九尾狐 2024-10-06 01:47:53

这是使用 awk 的解决方案:

awk '{print; if(getline < "file2") print}' file1

产生以下输出:

line 1 from file1
line 1 from file2
line 2 from file1
line 2 from file2
...etc

如果您想向输出添加一些额外的格式(例如,如果您想标记每一行),则使用 awk 会很有用基于它来自哪个文件:

awk '{print "1: "$0; if(getline < "file2") print "2: "$0}' file1

产生以下输出:

1: line 1 from file1
2: line 1 from file2
1: line 2 from file1
2: line 2 from file2
...etc

注意:此代码假定 file1 的长度大于或等于 file2。

如果 file1 包含的行数多于 file2,并且您希望在 file2 完成后输出空行,请在 getline 测试中添加 else 子句:

awk '{print; if(getline < "file2") print; else print ""}' file1

awk '{print "1: "$0; if(getline < "file2") print "2: "$0; else print"2: "}' file1

Here's a solution using 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 '{print "1: "$0; if(getline < "file2") print "2: "$0}' file1

produces this output:

1: line 1 from file1
2: line 1 from file2
1: line 2 from file1
2: line 2 from file2
...etc

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:

awk '{print; if(getline < "file2") print; else print ""}' file1

or

awk '{print "1: "$0; if(getline < "file2") print "2: "$0; else print"2: "}' file1
鹿港巷口少年归 2024-10-06 01:47:53

@Sujoy 的回答指出了一个有用的方向。您可以添加行号、排序和删除行号:

(cat -n file1 ; cat -n file2 )  | sort -n  | cut -f2-

请注意(我感兴趣)如果您使用的命令输出不是静态文件,而运行速度可能比静态文件慢或快,则需要做更多工作才能正确排序。彼此。在这种情况下,除了行号之外,您还需要添加/排序/删除另一个标签:

(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-

@Sujoy's answer points in a useful direction. You can add line numbers, sort, and strip the line numbers:

(cat -n file1 ; cat -n file2 )  | sort -n  | cut -f2-

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:

(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-
若无相欠,怎会相见 2024-10-06 01:47:53

使用 GNU sed:

sed 'R file2' file1

输出:

line1.1
line2.1
line1.2
line2.2
line1.3
line2.3

With GNU sed:

sed 'R file2' file1

Output:

line1.1
line2.1
line1.2
line2.2
line1.3
line2.3
逆夏时光 2024-10-06 01:47:53

这是一种 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.

不打扰别人 2024-10-06 01:47:53
cat file1 file2 |sort -t. -k 2.1

这里指定分隔符是“.”。我们正在对第二个字段的第一个字符进行排序。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文