![如何使用 awk 解析特定列并仅保留一部分](https://linux22.com/image/182060/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20awk%20%E8%A7%A3%E6%9E%90%E7%89%B9%E5%AE%9A%E5%88%97%E5%B9%B6%E4%BB%85%E4%BF%9D%E7%95%99%E4%B8%80%E9%83%A8%E5%88%86.png)
我有一个像这样的文件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
有人知道如何解决这个问题吗?谢谢