perl - Unix 加入两个以上的文件

标签 perl text join paste cut

我有三个文件,每个文件都有一个 ID 和一个值。

sdt5z@fir-s:~/test$ ls
a.txt  b.txt  c.txt
sdt5z@fir-s:~/test$ cat a.txt 
id1 1
id2 2
id3 3
sdt5z@fir-s:~/test$ cat b.txt 
id1 4
id2 5
id3 6
sdt5z@fir-s:~/test$ cat c.txt 
id1 7
id2 8
id3 9

我想创建一个看起来像这样的文件...
id1 1 4 7
id2 2 5 8
id3 3 6 9

...最好使用单个命令。

我知道加入和粘贴命令。 Paste 每次都会复制 id 列:
sdt5z@fir-s:~/test$ paste a.txt b.txt c.txt 
id1 1   id1 4   id1 7
id2 2   id2 5   id2 8
id3 3   id3 6   id3 9

加入效果很好,但一次只能处理两个文件:
sdt5z@fir-s:~/test$ join a.txt b.txt 
id1 1 4
id2 2 5
id3 3 6
sdt5z@fir-s:~/test$ join a.txt b.txt c.txt 
join: extra operand `c.txt'
Try `join --help' for more information.

我也知道 paste 可以通过使用“-”将 STDIN 作为参数之一。例如,我可以使用以下方法复制 join 命令:
sdt5z@fir-s:~/test$ cut -f2 b.txt | paste a.txt -
id1 1   4
id2 2   5
id3 3   6

但我仍然不确定如何修改它以容纳三个文件。

由于我是在 perl 脚本中执行此操作,因此我知道我可以将其放入 foreach 循环中,例如 join file1 file2 > tmp1、join tmp1 file3 > tmp2 等。但是这会变得困惑,我想用单线做到这一点。

最佳答案

join a.txt b.txt|join - c.txt
应该足够了

关于perl - Unix 加入两个以上的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9212893/

相关文章:

Perl 隐藏输出

perl - 在Perl中,有什么方法可以使用 “constant”作为哈希键吗?

arrays - 创建根据另一个变量的内容命名的 Perl 哈希值

text - 查找最长可能重复字符串的实用程序

java - 生成大量字母以供将来操作的最佳方法

mysql - 如何连接同一两个表中的多条记录

sql - 涉及连接和having(或where)子句的BigQuery嵌套挑战

perl - HTTP::Request 和文字 %2B

linux - 如何从文件中选择随机行

mysql - 了解 SQL 中的笛卡尔积