在文件之间匹配列,并使用terminal/powershell/命令行Bash中的数据组合生成文件
我有两个不同长度的.txt文件,希望执行以下操作: 如果文件1的第1列中的值出现在文件3的第1列中,则打印文件2的第2列,然后打印与文件1对应的整行 已经尝试了awk的排列,但是我到目前为止还没有成功 谢谢大家! 文件1:在文件之间匹配列,并使用terminal/powershell/命令行Bash中的数据组合生成文件,bash,powershell,awk,terminal,Bash,Powershell,Awk,Terminal,我有两个不同长度的.txt文件,希望执行以下操作: 如果文件1的第1列中的值出现在文件3的第1列中,则打印文件2的第2列,然后打印与文件1对应的整行 已经尝试了awk的排列,但是我到目前为止还没有成功 谢谢大家! 文件1: MARKERNAME EA NEA BETA SE 10:1000706 T C -0.021786390809225 0.519667838651725 1:715265 G C 0.0310128798578049 0.0403763946716293 10:100204
MARKERNAME EA NEA BETA SE
10:1000706 T C -0.021786390809225 0.519667838651725
1:715265 G C 0.0310128798578049 0.0403763946716293
10:1002042 CCTT C 0.0337857775471699 0.0403300629299562
文件2:
CHR:BP SNP CHR BP GENPOS ALLELE1 ALLELE0 A1FREQ INFO
1:715265 rs12184267 1 715265 0.0039411 G C 0.964671
1:715367 rs12184277 1 715367 0.00394384 A G 0.964588
所需文件3:
SNP MARKERNAME EA NEA BETA SE
rs12184267 1:715265 G C 0.0310128798578049 0.0403763946716293
尝试:
awk -F'|' 'NR==FNR { a[$1]=1; next } ($1 in a) { print $3, $0 }' file1 file2
awk 'NR==FNR{A[$1]=$2;next}$0 in A{$0=A[$0]}1' file1 file2
有了你们展示的样品,你们能试一下下面的吗
awk '
FNR==1{
if(++count==1){ col=$0 }
else{ print $2,col }
next
}
FNR==NR{
arr[$1]=$0
next
}
($1 in arr){
print $2,arr[$1]
}
' file1 file2
说明:增加对以上内容的详细说明
awk ' ##Starting awk program from here.
FNR==1{ ##Checking condition if this is first line of file(s).
if(++count==1){ col=$0 } ##Checking if count is 1 then set col as current line.
else{ print $2,col } ##Checking if above is not true then print 2nd field and col here.
next ##next will skip all further statements from here.
}
FNR==NR{ ##This will be TRUE when file1 is being read.
arr[$1]=$0 ##Creating arr with 1st field index and value is current line.
next ##next will skip all further statements from here.
}
($1 in arr){ ##Checking condition if 1st field present in arr then do following.
print $2,arr[$1] ##Printing 2nd field, arr value here.
}
' file1 file2 ##Mentioning Input_files name here.
有了你们展示的样品,你们能试一下下面的吗
awk '
FNR==1{
if(++count==1){ col=$0 }
else{ print $2,col }
next
}
FNR==NR{
arr[$1]=$0
next
}
($1 in arr){
print $2,arr[$1]
}
' file1 file2
说明:增加对以上内容的详细说明
awk ' ##Starting awk program from here.
FNR==1{ ##Checking condition if this is first line of file(s).
if(++count==1){ col=$0 } ##Checking if count is 1 then set col as current line.
else{ print $2,col } ##Checking if above is not true then print 2nd field and col here.
next ##next will skip all further statements from here.
}
FNR==NR{ ##This will be TRUE when file1 is being read.
arr[$1]=$0 ##Creating arr with 1st field index and value is current line.
next ##next will skip all further statements from here.
}
($1 in arr){ ##Checking condition if 1st field present in arr then do following.
print $2,arr[$1] ##Printing 2nd field, arr value here.
}
' file1 file2 ##Mentioning Input_files name here.
欢迎来到SO,请在您的问题中添加3个简单的内容。第一个-输入样本,第二个-输出样本,第三个-你以代码的形式所做的努力,这将使你的问题变得清晰。不是我的反对票,顺便说一句,谢谢你。@CEL:你没有发布任何代码,所以很难讨论这个问题。抱歉-已修改。谢谢大家!@CEL,这是对你的问题的很好的编辑,谢谢你这么做。@RavinderSingh13如果col 1 MAKERNAME file 1=col 1 file 2 CHR:BP,打印col 2 file 2 SNP和所有文件1数据欢迎这么做,请在你的问题中添加3件简单的事情。第一个-输入样本,第二个-输出样本,第三个-你以代码的形式所做的努力,这将使你的问题变得清晰。不是我的反对票,顺便说一句,谢谢你。@CEL:你没有发布任何代码,所以很难讨论这个问题。抱歉-已修改。谢谢大家!@CEL,这是对您的问题的很好的编辑,谢谢您这么做。@RavinderSingh13如果col 1 MAKERNAME file 1=col 1 file 2 CHR:BP,请打印col 2 file 2 SNP和所有文件1数据