awk使用另一个文件的内容更新out文件字段中的值

awk使用另一个文件的内容更新out文件字段中的值,awk,Awk,在下面的out.txt中,我试图使用awk更新$9的内容。out.txt由awk在管道|之前创建。如果$9包含+或-,则out.txt的$8用作在文件2的$2中查找的键。当找到匹配项(总是有一个)时,该文件2的$3值用于更新out.txt的$9,并用分隔:。因此out.txt中的原始+6将是+6:NM\u 005101.3。下面的awk很接近,但是|后面有语法错误,我似乎无法修复。谢谢:) out.txt制表符分隔 R_Index Chr Start End Ref Alt Func.IDP

在下面的
out.txt
中,我试图使用
awk
更新
$9
的内容。
out.txt
awk
在管道
|
之前创建。如果
$9
包含
+
-
,则
out.txt的
$8
用作在
文件2的
$2
中查找的键。当找到匹配项(总是有一个)时,该
文件2的
$3
值用于更新
out.txt的
$9
,并用
分隔:
。因此
out.txt中的原始
+6
将是
+6:NM\u 005101.3
。下面的
awk
很接近,但是
|
后面有语法错误,我似乎无法修复。谢谢:)

out.txt
制表符分隔

R_Index Chr Start   End Ref Alt Func.IDP.refGene    Gene.IDP.refGene    GeneDetail.IDP.refGene  Inheritence ExonicFunc.IDP.refGene  AAChange.IDP.refGene
1   chr1    948846  948846  -   A   upstream    ISG15   -0     .    .   .
2   chr1    948870  948870  C   G   UTR5    ISG15   NM_005101.3:c.-84C>G    .   .
4   chr1    949925  949925  C   T   downstream  ISG15   +6  .   .   .
5   chr1    207646923   207646923   G   A   intronic    CR2 >50 .   .   .
8   chr1    948840  948840  -   C   upstream    ISG15   -6  .   .   .
2 ISG15 NM_005101.3 948846-948956 949363-949919
文件2
空格分隔

R_Index Chr Start   End Ref Alt Func.IDP.refGene    Gene.IDP.refGene    GeneDetail.IDP.refGene  Inheritence ExonicFunc.IDP.refGene  AAChange.IDP.refGene
1   chr1    948846  948846  -   A   upstream    ISG15   -0     .    .   .
2   chr1    948870  948870  C   G   UTR5    ISG15   NM_005101.3:c.-84C>G    .   .
4   chr1    949925  949925  C   T   downstream  ISG15   +6  .   .   .
5   chr1    207646923   207646923   G   A   intronic    CR2 >50 .   .   .
8   chr1    948840  948840  -   C   upstream    ISG15   -6  .   .   .
2 ISG15 NM_005101.3 948846-948956 949363-949919
所需输出“制表符分隔”

R_Index Chr Start   End Ref Alt Func.IDP.refGene    Gene.IDP.refGene    GeneDetail.IDP.refGene  Inheritence ExonicFunc.IDP.refGene  AAChange.IDP.refGene
1   chr1    948846  948846  -   A   upstream    ISG15   -0:NM_005101.3  .   .   .
2   chr1    948870  948870  C   G   UTR5    ISG15   NM_005101.3:c.-84C>G    .   .
4   chr1    949925  949925  C   T   downstream  ISG15   +6:NM_005101.3  .   .   .
5   chr1    207646923   207646923   G   A   intronic    CR2 >50 .   .   .
8   chr1    948840  948840  -   C   upstream    ISG15   -6:NM_005101.3  .   .   .
说明

lines 1, 3, 5 `$9` updated with`: ` and value of `$3` in `file2`
line 2 and 4 are skipped as these do not have a `+` or `-` in them 
awk

awk -v extra=50 -v OFS='\t' '
NR == FNR {
count[$2] = $1
for(i = 1; i <= $1; i++) {
low[$2, i] = $(2 + 2 * i)
high[$2, i] = $(3 + 2 * i)
mid[$2, i] = (low[$2, i] + high[$2, i]) / 2
}
next
}
    FNR != 1 && $9 == "." && $12 == "." && $8 in count {
    for(i = 1; i <= count[$8]; i++)
    if($4 >= (low[$8, i] - extra) && $4 <= (high[$8, i] + extra)) {
    if($4 > mid[$8, i]) {
    sign = "+"
    value = high[$8, i]
} 
    else {
    sign = "-"
    value = low[$8, i]
}
    diff = (value > $4) ? value - $4 : $4 - value
    $9 = (diff > 50) ? ">50" : (sign diff)
    break
}
   if(i > count[$8]) {
   $9 = ">50"
}
   }
   1
   ' FS='[- ]' file2 FS='\t' file1 | awk if($6 == "-" || $6 == "+") printf ":" ; 'FNR==NR {a[$2]=$3; next} a[$8]{$3=a[$8]}1' OFS='\t' file2 > final.txt
bash: syntax error near unexpected token `('
awk-v额外=50-v OFS='\t''
NR==FNR{
计数[$2]=$1
对于(i=1;i$4)?值-$4:$4-值
$9=(差异>50)?“>50”:(符号差异)
打破
}
如果(i>计数[$8]){
$9 = ">50"
}
}
1.
'FS='[-]'file2 FS='\t'file1 | awk如果($6==“-”| |$6==“+”)printf:“;”FNR==NR{a[$2]=$3;next}a[$8]{$3=a[$8]}1'OFS='\t'file2>final.txt
bash:意外标记“(”附近出现语法错误

据我所知,您的awk代码是正常的,而您的bash用法是错误的

FS='[- ]' file2 FS='\t' file1 |
  awk if($6 == "-" || $6 == "+")
      printf ":" ;
  'FNR==NR {a[$2]=$3; next}
   a[$8]{$3=a[$8]}1' OFS='\t' file2 > final.txt
bash: syntax error near unexpected token `('

我不知道该怎么做。但这是肯定的:在第二行,awk代码需要被引用(
awk'if(…
)。bash错误消息源于bash正在解释(未引用的)awk代码,并且
之后不是有效的shell脚本标记如果

是,我需要缩进是否有标准格式或是否按块缩进或其他方式。谢谢:)。我在帖子中缩进了
awk
,并消除了
if
语法错误,但得到了一个意外的标记。我是否正确调用了第二个
awk
?第二个
awk'执行了对
out.txt`到
file2
的查找,然后更新。谢谢:)。虽然您在此处或那里插入了几行代码,但缩进仍然没有用。当您缩进代码时,它是为了显示行之间的逻辑关系,以便为
if
编码
中的内容,而
块将缩进,等等。请编写第二个脚本的语法,idk如何知道语法to编写第一个脚本,但不知道如何编写第二个脚本,但首先要更改您必须更改的内容
awk'$6==”-“| |$6==”+“{printf”:“}FNR==NR{a[$2]=$3;下一个}a[$8]{$3=a[$8]}1”
idk,如果这在语义上有意义,但应该删除语法错误消息FWIW。我将阅读更多有关缩进的内容……谢谢:).非常感谢你们两位:)。