如何使用 awk 解析特定列并仅保留一部分

如何使用 awk 解析特定列并仅保留一部分

我有一个像这样的文件A.txt(字段分隔符= ,):

Kit Batch Export
Software Version = NO_v1
Date And Time of Export =
Experiment Name = MY20195334_MOL_2014_20201006Run180017
Instrument Software Version =
Instrument Type = Cji
Instrument Serial Number =
Run Start Date =
Run End Date =
Run Operator =
Batch Status = VALID
Method = Nov
Date And Time of Export,Batch ID,Sample Name,Well,Sample Type,Status,Interpretive Result,Action*,Curve analysis,EC,CH
,MY20195334_MOL_2014_20201006Run180017,2061571293,A01,Unkn-01,VALID,,,
,MY20195334_MOL_2014_20201006Run180017,2061584371,A02,Unkn-09,VALID,,,

*See Kit IFU for reporting activities.
CE-IVD
For In Vitro Diagnostic Use.

我想通过仅保留第一列之后_和第三列之前的内容来解析第二列_,如下所示:

Kit Batch Export
Software Version = NO_v1
Date And Time of Export =
Experiment Name = MY20195334_MOL_2014_20201006Run180017
Instrument Software Version =
Instrument Type = Cji
Instrument Serial Number =
Run Start Date =
Run End Date =
Run Operator =
Batch Status = VALID
Method = Nov
Date And Time of Export,Batch ID,Sample Name,Well,Sample Type,Status,Interpretive Result,Action*,Curve analysis,EC,CH
,MOL_2014,2061571293,A01,Unkn-01,VALID,,,
,MOL_2014,2061584371,A02,Unkn-09,VALID,,,

*See Kit IFU for reporting activities.
CE-IVD
For In Vitro Diagnostic Use.

为了做到这一点,我尝试这样的事情:

awk 'BEGIN{FS=OFS=","} NR==4{split($1,a,"_")} NR>13{$2=a[2]"_"a[3]} 1' A.txt > B.txt

它有效,但打印继续在文件末尾进行,如下所示:

Kit Batch Export
Software Version = NO_v1
Date And Time of Export =
Experiment Name = MY20195334_MOL_2014_20201006Run180017
Instrument Software Version =
Instrument Type = Cji
Instrument Serial Number =
Run Start Date =
Run End Date =
Run Operator =
Batch Status = VALID
Method = Nov
Date And Time of Export,Batch ID,Sample Name,Well,Sample Type,Status,Interpretive Result,Action*,Curve analysis,EC,CH
,MOL_2014,2061571293,A01,Unkn-01,VALID,,,
,MOL_2014,2061584371,A02,Unkn-09,VALID,,,
,MOL_2014
*See Kit IFU for reporting activities.,MOL_2014
CE-IVD,MOL_2014
For In Vitro Diagnostic Use.,MOL_2014

有人知道如何解决这个问题吗?谢谢

相关内容