awkを使用してあるファイルから別のファイルにデータを印刷する方法

awkを使用してあるファイルから別のファイルにデータを印刷する方法

ファイル (A.txt; sep="\t") があります。

blabla  lili
bloblo  lulu

C.txtを生成するために、B.txtの特定の場所にA.txtのいくつかのデータを印刷したいと思います。

B.txt(9月=","):

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.

秘密は、A.txtのデータがB.txtの$ 2に印刷されるように、B.txtの区切り文字として「=」を設定することです。私は同様のことを試しました:

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

おすすめ記事