bash 合并两个文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6408635/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:12:43  来源:igfitidea点击:

merging two files

bashshellunix

提问by Angelo

I have two files (tab delimited) one file is has 4 columns and n number of rows and second file has 2 columns and n number of rows.

我有两个文件(制表符分隔),一个文件有 4 列和 n 行,第二个文件有 2 列和 n 行。

column 4 of first file is identical to column 2 of second file.

第一个文件的第 4 列与第二个文件的第 2 列相同。

I want to have a third file which contains first four columns from file 1 and column 5 from file 2.

我想要第三个文件,其中包含文件 1 中的前四列和文件 2 中的第 5 列。

Any suggestions for one line bash script.

对一行 bash 脚本的任何建议。

回答by Cédric Julien

try with join

尝试 join

join FILE1 FILE2 -1 4 -2 2 -t"tab"

join FILE1 FILE2 -1 4 -2 2 -t"tab"

to express a join between the files FILE1 and FILE2 based on the 4th field (-1 4) of FILE1 and the 2nd field (-2 2) of FILE2

根据 FILE1 的第 4-1 4个字段 ( -2 2) 和 FILE2的第 2 个字段 ( )表示文件 FILE1 和 FILE2 之间的连接

回答by Fredrik Pihl

Have a look at the joincommand, see guide here

查看join命令,请参阅此处的指南

回答by MacMark

For tabulator try

对于制表机尝试

join -t \t files1 …