If statement 如何基于多列中的数据合并两个文件?

If statement 如何基于多列中的数据合并两个文件?,if-statement,awk,multiple-columns,If Statement,Awk,Multiple Columns,我有两个独立的文件,每个文件包含不同数量的列,我想根据多列中的数据合并这些列 文件1 VMNF01000015.1 1769465 1769675 . . - Focub_II5_mimp_1 VMNF01000014.1 3225875 3226081 . . + Focub_II5_mimp_1 VMNF01000014.1 3226046 3226081 . . - Focub_II5_mimp_1 VMNF01000014.1 3585246

我有两个独立的文件,每个文件包含不同数量的列,我想根据多列中的数据合并这些列

文件1

VMNF01000015.1  1769465 1769675 .   .   -   Focub_II5_mimp_1
VMNF01000014.1  3225875 3226081 .   .   +   Focub_II5_mimp_1
VMNF01000014.1  3226046 3226081 .   .   -   Focub_II5_mimp_1
VMNF01000014.1  3585246 3585281 .   .   -   Focub_II5_mimp_1
VMNF01000014.1  3692468 3692503 .   .   -   Focub_II5_mimp_1
VMNF01000014.1  3715380 3715415 .   .   +   Focub_II5_mimp_1
VMNF01000014.1  2872478 2872511 .   .   -   Focub_II5_mimp_1
文件2

首先,我认为我需要在file2中创建另外两列,用“-”分隔数字,并为“(*)”创建一个新列,但我无法计算如何在不替换“(-”)的情况下分隔数字。到目前为止,我一直在使用以下命令:

awk '{gsub("-","\t",$2);print;}'
awk 'NR==FNR {a[$1]=$3; next} {print $1,$2,$3,$4,$5,$6,$7,a[$1];}' file2 file1 > file3. 
完成后,我想将file2中的最后一列添加到file1中。我已经能够使用以下命令执行此操作:

awk '{gsub("-","\t",$2);print;}'
awk 'NR==FNR {a[$1]=$3; next} {print $1,$2,$3,$4,$5,$6,$7,a[$1];}' file2 file1 > file3. 
但是,数据不匹配。根据第1列中的条目进行匹配。在许多情况下,第1列中的数据是相同的,因此文件3第8列中的数据只匹配其中一个条目,而与文件1第2列或第3列中的数据不匹配,例如

文件3:

VMNF01000015.1  1769465 1769675 .   .   -   Focub_II5_mimp_1    gtacttcagcctggattcaaacttattgcatcccactgta
VMNF01000014.1  3225875 3226081 .   .   +   Focub_II5_mimp_1    gtacttcagcctggattcaaacttattgcatcccactgta
VMNF01000014.1  3226046 3226081 .   .   -   Focub_II5_mimp_1    gtacttcagcctggattcaaacttattgcatcccactgta
VMNF01000014.1  3585246 3585281 .   .   -   Focub_II5_mimp_1    gtacttcagcctggattcaaacttattgcatcccactgta
VMNF01000014.1  3692468 3692503 .   .   -   Focub_II5_mimp_1    gtacttcagcctggattcaaacttattgcatcccactgta
VMNF01000014.1  3715380 3715415 .   .   +   Focub_II5_mimp_1    gtacttcagcctggattcaaacttattgcatcccactgta
VMNF01000014.1  2872478 2872511 .   .   -   Focub_II5_mimp_1    gtacttcagcctggattcaaacttattgcatcccactgta
即使我能够分离file2第2列中的数据,我仍然会遇到相同的问题,因为第2列中的数据在某些情况下是相同的。我需要的是这样的代码:解析第2列中的数据(见下文)

然后:

如果file1中的$1、$2、$3与file2中的$1、$2、$3匹配,则从file1中打印$1、$2、$3、$4、$5、$6、$7,并从file2中添加$5

我该怎么做?我知道awk可以使用if语句,但我不知道如何在awk中使用它们


有什么建议吗

你能试试下面的吗

awk '
FNR==NR{
  split($2,array,"[-(]")
  mainarray[$1,array[1],array[2]]=$NF
  next
}
(($1,$2,$3) in mainarray){
  print $0,mainarray[$1,$2,$3]
}
'  Input_file2  Input_file1
第二种解决方案:由于OP在上述代码中出现错误,所以在上述代码中做了一些更改

awk '
FNR==NR{
  split($2,array,"[-(]")
  key=$1 OFS array[1] OFS array[2]
  mainarray[key]=$NF
  next
}
{ key = $1 OFS $2 OFS $3 }
(key in mainarray){
  print $0,mainarray[key]
}
'  Input_file2  Input_file1
解释:添加上述代码的详细解释

awk '                                       ##Starting awk program from here.
FNR==NR{                                    ##Checking condition FNR==NR when  Input_file2 is being read.
  split($2,array,"[-(]")                    ##Splitting 2nd field into an array named array where delimiter is - OR (
  mainarray[$1,array[1],array[2]]=$NF       ##Creating mainarray index of $1,array[1],array[2] and value is current line is last field.
  next                                      ##next will skip all further statements from here.
}
(($1,$2,$3) in mainarray){                  ##Checking condition if $1,$2,$3 of current line is present in mainaarray.
  print $0,mainarray[$1,$2,$3]              ##Printing current line with value of mainarray with index of $1,$2,$3
}
'  Input_file2  Input_file1                 ##Mentioning Input_file names here.

你能试试下面的吗

awk '
FNR==NR{
  split($2,array,"[-(]")
  mainarray[$1,array[1],array[2]]=$NF
  next
}
(($1,$2,$3) in mainarray){
  print $0,mainarray[$1,$2,$3]
}
'  Input_file2  Input_file1
第二种解决方案:由于OP在上述代码中出现错误,所以在上述代码中做了一些更改

awk '
FNR==NR{
  split($2,array,"[-(]")
  key=$1 OFS array[1] OFS array[2]
  mainarray[key]=$NF
  next
}
{ key = $1 OFS $2 OFS $3 }
(key in mainarray){
  print $0,mainarray[key]
}
'  Input_file2  Input_file1
解释:添加上述代码的详细解释

awk '                                       ##Starting awk program from here.
FNR==NR{                                    ##Checking condition FNR==NR when  Input_file2 is being read.
  split($2,array,"[-(]")                    ##Splitting 2nd field into an array named array where delimiter is - OR (
  mainarray[$1,array[1],array[2]]=$NF       ##Creating mainarray index of $1,array[1],array[2] and value is current line is last field.
  next                                      ##next will skip all further statements from here.
}
(($1,$2,$3) in mainarray){                  ##Checking condition if $1,$2,$3 of current line is present in mainaarray.
  print $0,mainarray[$1,$2,$3]              ##Printing current line with value of mainarray with index of $1,$2,$3
}
'  Input_file2  Input_file1                 ##Mentioning Input_file names here.

我注意到,在某些情况下,也需要(-)或(+)来确保第8列中的正确顺序,有没有办法也包括这一点?可以通过向键添加另一个数组来完成吗?@Jpike,最好打开一个新问题(连同您的努力和所有示例),通常鼓励使用1线程1问题模式,如果您喜欢,可以接受此答案,干杯。我注意到在某些情况下(-)或(+)也需要确保第8列中的正确顺序,是否有方法将其包括在内?可以通过向key添加另一个数组来完成吗?@Jpike,最好打开一个新问题(连同您的努力和所有示例),通常鼓励使用1线程1问题模式,如果您喜欢,可以接受此答案,干杯。