如果在 awk 中找到模式如何打印数据

如果在 awk 中找到模式如何打印数据

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

kit
Software Version =
Date And Time of Export =
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
,,205920777.1,A01,Unkn-01,,,
,,neg5,A02,Neg Ctrl-01,,,
,,pos6,A03,Pos Ctrl-01,,,
,,,,,,,,,,
*reporting.

如果“样本类型”列包含模式:“患者”如果“Unkn”;我想在“解释结果”列中打印结果;如果“否定”则为“NC”;如果“Pos”则为“PC”。为了获得以下输出(B.txt ; sep=","):

kit
Software Version =
Date And Time of Export =
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
,,205920777.1,A01,Unkn-01,,Patient,,
,,neg5,A02,Neg Ctrl-01,,NC,,
,,pos6,A03,Pos Ctrl-01,,PC,,
,,,,,,,,,,
*reporting.

我尝试过类似的事情:

awk -F',' -v OFS="," '(NR>1 && $5="Unkn"*){print ...}' A.txt > B.txt

但我没能解决这个问题。有人可以有一个想法吗?

谢谢

答案1

正如上面所评论的,您需要==进行比较。但是使用~运算符将​​字段与正则表达式进行比较,这就是它的用途。

pat.awk

BEGIN{FS=OFS=","}
$5 ~ /Unkn/{$7="Patient,"}
$5 ~ /Neg/{$7="NC,"}
$5 ~ /Pos/{$7="PC,"}
{print}
$ awk -f pat.awk A.txt 
kit
Software Version =
Date And Time of Export =
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
,,205920777.1,A01,Unkn-01,,Patient,,
,,neg5,A02,Neg Ctrl-01,,NC,,
,,pos6,A03,Pos Ctrl-01,,PC,,
,,,,,,,,,,
*reporting.

如果您更喜欢单行:

awk -F ',' -v OFS=',' '$5~/Unkn/{$7="Patient,"}$5~/Neg/{$7="NC,"}$5~/Pos/{$7="PC,"}1' A.txt

相关内容