如何在 awk 中从列中的一行打印模式

如何在 awk 中从列中的一行打印模式

我有一个文件 A.txt (sep=","):

kit
Software Version =
Date And Time of Export = 07/02/2020 13:44:11 UTC
Experiment Name =
Instrument Software Version =
Instrument Type = CFX
Instrument Serial Number =
Run Start Date =
Run End Date =
Run Operator =
Batch Status = VALID
Method = Novaprime
Date And Time of Export,Batch ID,Sample Name,Well,Sample Type,Status,Interpretive Result,Action*,Curve analysis
,taq,205920777.1,A01,Unkn-01
,taq,neg5,A02,Unkn-09
,,,,,,,,,,
*reporting.

我想在第 13 行之后的文件第一列中添加第 3 行的模式,如下所示:

kit
Software Version =
Date And Time of Export = 07/02/2020 13:44:11 UTC
Experiment Name =
Instrument Software Version =
Instrument Type = CFX
Instrument Serial Number =
Run Start Date =
Run End Date =
Run Operator =
Batch Status = VALID
Method = Novaprime
Date And Time of Export,Batch ID,Sample Name,Well,Sample Type,Status,Interpretive Result,Action*,Curve analysis
07/02/2020 13:44:11 UTC,taq,205920777.1,A01,Unkn-01
07/02/2020 13:44:11 UTC,taq,neg5,A02,Unkn-09
,,,,,,,,,,
*reporting.

技巧是设置“=”作为B.txt的分隔符,以便将A.txt的数据打印在B.txt的$1中。我尝试过类似的事情:

awk 'BEGIN{OFS=FS=" = "} NR==3{stuff} } 1' A.txt > B.txt

但我没弄清楚。有任何想法吗?

谢谢

答案1

尝试:

awk -F' = ' 'NR==3{a=$2} {if(NR<14)print; else print a $0}' A.txt

使用您的示例输入:

$ awk -F' = ' 'NR==3{a=$2} {if(NR<14)print; else print a $0}' A.txt
kit
Software Version =
Date And Time of Export = 07/02/2020 13:44:11 UTC
Experiment Name =
Instrument Software Version =
Instrument Type = CFX
Instrument Serial Number =
Run Start Date =
Run End Date =
Run Operator =
Batch Status = VALID
Method = Novaprime
Date And Time of Export,Batch ID,Sample Name,Well,Sample Type,Status,Interpretive Result,Action*,Curve analysis
07/02/2020 13:44:11 UTC,taq,205920777.1,A01,Unkn-01
07/02/2020 13:44:11 UTC,taq,neg5,A02,Unkn-09

多行格式

对于那些喜欢将命令分散在多行中的人:

awk -F' = ' '
    NR==3{
        a=$2
    }

    {
        if(NR<14)
            print
        else
            print a $0
    }
    ' A.txt

怎么运行的

  • -F' = '

    这会将字段分隔符设置为=

  • NR==3{a=$2}

    对于第三行,这会将第二个字段保存在变量中a

  • if(NR<14)print; else print a $0

    对于小于 14 的行号,这将按原样打印该行。对于其余行,这会打印a前面带有变量的行。

更新:从第 14 行添加时间到除最后四行之外的所有行

awk -F' = ' 'NR==3{t=$2} NR<14{print;next} NR>17{print t d} {d=c;c=b;b=a;a=$0} END{print d ORS c ORS b ORS a}' A.txt

输入文件示例:

$ cat A.txt
kit
Software Version =
Date And Time of Export = 07/02/2020 13:44:11 UTC
Experiment Name =
Instrument Software Version =
Instrument Type = CFX
Instrument Serial Number =
Run Start Date =
Run End Date =
Run Operator =
Batch Status = VALID
Method = Novaprime
Date And Time of Export,Batch ID,Sample Name,Well,Sample Type,Status,Interpretive Result,Action*,Curve analysis
,taq,205920777.1,A01,Unkn-01
,taq,neg5,A02,Unkn-09
end1
end2
end3
end4

对应输出:

$ awk -F' = ' 'NR==3{t=$2} NR<14{print;next} NR>17{print t d} {d=c;c=b;b=a;a=$0} END{print d ORS c ORS b ORS a}' A.txt
kit
Software Version =
Date And Time of Export = 07/02/2020 13:44:11 UTC
Experiment Name =
Instrument Software Version =
Instrument Type = CFX
Instrument Serial Number =
Run Start Date =
Run End Date =
Run Operator =
Batch Status = VALID
Method = Novaprime
Date And Time of Export,Batch ID,Sample Name,Well,Sample Type,Status,Interpretive Result,Action*,Curve analysis
07/02/2020 13:44:11 UTC,taq,205920777.1,A01,Unkn-01
07/02/2020 13:44:11 UTC,taq,neg5,A02,Unkn-09
end1
end2
end3
end4

答案2

“线路无关”版本

awk -F, -v OFS="," '$0~/UTC/{split($0,ar,"= ")}$2=="taq"{$1=ar[2]}1' file

kit
Software Version =
Date And Time of Export = 07/02/2020 13:44:11 UTC
Experiment Name =
Instrument Software Version =
Instrument Type = CFX
Instrument Serial Number =
Run Start Date =
Run End Date =
Run Operator =
Batch Status = VALID
Method = Novaprime
Date And Time of Export,Batch ID,Sample Name,Well,Sample Type,Status,Interpretive Result,Action*,Curve analysis
07/02/2020 13:44:11 UTC,taq,205920777.1,A01,Unkn-01
07/02/2020 13:44:11 UTC,taq,neg5,A02,Unkn-09
,,,,,,,,,,
*reporting.

设置FS和 ,OFS,将日期拆分到ar您找到的行上的数组中UTC,然后将其插入到$1任意位置$2=="taq"

相关内容