如何使用awk将数据从一个文件打印到另一个文件

如何使用awk将数据从一个文件打印到另一个文件

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

blabla  lili
bloblo  lulu

我想在B.txt中的特定位置打印A.txt的某些数据,以生成C.txt。

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
,,,,,,,,,,
*reporting.

C.txt 示例 (sep=","):

kit
Software Version = lili
Date And Time of Export =
Experiment Name =
Instrument Software Version =
Instrument Type = CFX
Instrument Serial Number =
Run Start Date = lulu
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
,,,,,,,,,,
*reporting.

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

awk 'BEGIN{OFS=FS=" = "} NR==2{stuff} ; NR==8{stuff} } 1' A.txt B.txt > C.txt

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

谢谢

答案1

awk -F'\t' '
  FNR==NR{ a[NR]=$2; next }
  FNR==2{ print $0, a[1]; next }
  FNR==8{ print $0, a[2]; next }
  1
' A.txt B.txt > C.txt

或者用更有意义的描述:

awk -F'\t' '
  FNR==NR{ a[NR]=$2; next }
  $1=="Software Version"{ print $0, a[1]; next }
  $1=="Run Start Date"{ print $0, a[2]; next }
  1
' A.txt FS=" =" B.txt > C.txt

相关内容