Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 匹配两个文件的第一列中的值,并将匹配行连接到新文件中_Bash_Awk_Grep_Match - Fatal编程技术网

Bash 匹配两个文件的第一列中的值,并将匹配行连接到新文件中

Bash 匹配两个文件的第一列中的值,并将匹配行连接到新文件中,bash,awk,grep,match,Bash,Awk,Grep,Match,我需要在file1.txt中查找第1列($1)中的字符串与file2.txt中第1列($1)中的字符串的匹配项。然后我想在新文件中加入匹配的行 cat file1.txt 1050008 5.156725968 8.404038296 124.9198605 3.23E-21 2.33E-17 38.57865782 3310747 5.631470026 8.581936875 124.6039122 3.34E-21 2.33E-17 38.55204806 5910

我需要在file1.txt中查找第1列($1)中的字符串与file2.txt中第1列($1)中的字符串的匹配项。然后我想在新文件中加入匹配的行

cat file1.txt
1050008 5.156725968 8.404038296 124.9198605 3.23E-21    2.33E-17    38.57865782
3310747 5.631470026 8.581936875 124.6039122 3.34E-21    2.33E-17    38.55204806
5910451 4.900364671 8.455329195 124.5720603 3.35E-21    2.33E-17    38.54935989
730156  5.565210738 8.48792701  122.2168789 4.28E-21    2.33E-17    38.34773989

cat file2.txt
4230037 ILMN Controls   ILMN_Controls   ERCC-00071  ILMN_333646 ERCC-00071  ERCC-00071
1050008 ILMN Controls   ILMN_Controls   ERCC-00009  ILMN_333584 ERCC-00009  ERCC-00009
5260356 ILMN Controls   ILMN_Controls   ERCC-00053  ILMN_333628 ERCC-00053  ERCC-00053
3310747 ILMN Controls   ILMN_Controls   ERCC-00144  ILMN_333719 ERCC-00144  ERCC-00144
5910451 ILMN Controls   ILMN_Controls   ERCC-00003  ILMN_333578 ERCC-00003  ERCC-00003
1710435 ILMN Controls   ILMN_Controls   ERCC-00138  ILMN_333713 ERCC-00138  ERCC-00138
1400612 ILMN Controls   ILMN_Controls   ERCC-00084  ILMN_333659 ERCC-00084  ERCC-00084
730156  ILMN Controls   ILMN_Controls   ERCC-00017  ILMN_333592 ERCC-00017  ERCC-00017
我希望输出文件如下所示:

out.txt
1050008 5.156725968 8.404038296 124.9198605 3.23E-21    2.33E-17    38.57865782 1050008 ILMN Controls   ILMN_Controls   ERCC-00009  ILMN_333584 ERCC-00009  ERCC-00009
3310747 5.631470026 8.581936875 124.6039122 3.34E-21    2.33E-17    38.55204806 3310747 ILMN Controls   ILMN_Controls   ERCC-00144  ILMN_333719 ERCC-00144  ERCC-00144
5910451 4.900364671 8.455329195 124.5720603 3.35E-21    2.33E-17    38.54935989 5910451 ILMN Controls   ILMN_Controls   ERCC-00003  ILMN_333578 ERCC-00003  ERCC-00003
730156  5.565210738 8.48792701  122.2168789 4.28E-21    2.33E-17    38.34773989 730156  ILMN Controls   ILMN_Controls   ERCC-00017  ILMN_333592 ERCC-00017  ERCC-00017
文件以制表符分隔,某些列中缺少值

file2.txt中有31列,超过47000行,我正试图在bash(OSX)中实现这一点


如果您有一个解决方案,我将非常感谢您能简要解释一下这些步骤,因为我对这一点非常陌生。

如果您不介意输出按第一列排序,那么您可以使用以下命令调用:

awk 'BEGIN {
  FS = OFS = "\t"
  }
NR == FNR {
  # while reading the 1st file
  # store its records in the array f
  f[$1] = $0
  next
  }
$1 in f {
  # when match is found
  # print all values
  print f[$1], $0
  }' file1 file2 

join check here:或here:+1获取一个好的脚本。但是,如果在任一文件的第一列中出现空格,或者其中一行中缺少该字段,并且OP明确指出某些字段缺少值,那么您应该使用
awk'BEGIN{FS=OFS=“\t”}…”
,以确保安全。这是一个很好的解决方案。有没有一种简单的方法来处理某些文件缺少行的情况?@IsaacTurner如果其中一个文件缺少行,这种方法应该可以工作,您有什么问题?@cmh我想填写缺少的值-我找到了这个问题的解决方案
join <(sort file1.txt) <(sort file2.txt) >out.txt