Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.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 如何在不影响标头的情况下使用此awk命令_Bash_Unix_Awk_Dataset - Fatal编程技术网

Bash 如何在不影响标头的情况下使用此awk命令

Bash 如何在不影响标头的情况下使用此awk命令,bash,unix,awk,dataset,Bash,Unix,Awk,Dataset,晚安。我有两个文件: 文件1-对于表型信息,第一列是ID,原始文件有400行: ID a b c d 215 2 25 13.8354303 15.2841303 222 2 25.2 15.8507278 17.2994278 216 2 28.2 13.0482192 14.4969192 223 11 15.4 9.2714745 11.6494745 文件2-使用SNPs信息,原始文件有400行,每行42000个字符 ID t u j l 215 2 0 2

晚安。我有两个文件:

文件1-对于表型信息,第一列是ID,原始文件有400行:

ID  a b  c          d 
215 2 25 13.8354303 15.2841303
222 2 25.2 15.8507278 17.2994278
216 2 28.2 13.0482192 14.4969192
223 11 15.4 9.2714745 11.6494745
文件2-使用SNPs信息,原始文件有400行,每行42000个字符

ID  t u j l
215 2 0 2 1 
222 2 0 1 1 
216 2 0 2 1 
223 2 0 2 2 
217 2 0 2 1 
218 0 2 0 2 
我需要从文件2中删除文件1中未出现的个人,例如:

ID  t u j l
215 2 0 2 1 
222 2 0 1 1 
216 2 0 2 1 
223 2 0 2 2
我使用了以下代码:

awk 'NR==FNR{a[$1]; next}$1 in a{print $0}' file2 file1 > file3
我可以得到这个输出(文件3):


但是我丢失了头,如何不丢失头?

要保留第二个文件的头,请添加一个
条件{action}
,如下所示:

awk 'NR==FNR {a[$1]; next}
     FNR==1  {print $0; next}  # <= this will print the header of file2.
     $1 in a {print $0}' file1 file2
awk'NR==FNR{a[$1];next}

FNR==1{print$0;next}#要保留第二个文件的头,请添加一个
条件{action}
,如下所示:

awk 'NR==FNR {a[$1]; next}
     FNR==1  {print $0; next}  # <= this will print the header of file2.
     $1 in a {print $0}' file1 file2
awk'NR==FNR{a[$1];next}

FNR==1{print$0;next}#使用样本数据
awk'NR==FNR{a[$1];在'file1 file2>file3
中的next}$1工作正常。使用样本数据
awk'NR==FNR{a[$1];在'file1 file2>file3
中的next}$1工作正常。