awk – How to Replace a String in One File if a Pattern is Present in Another File

awktext processing

I have a data file A.txt (field separator = \t) :

Well    Well Type   Well Name   Dye Target  
A1      Unknown     HIGH-001    FAM ViroFAM                 
A1      Unknown     HIGH-001    HEX ViroHEX

And a template file B.txt:

kit
Software Version = NOVA_v1
Date And Time of Export = 07/02/2020 13:44:11 UTC
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
,taq,205920777.1,A01,Unkn-01
,taq,neg5,A02,Unkn-09
,,,,,,,,,,
*reporting.

And I want to print replace the value after the =in the second line of B.txt with VIRO_v1, but only when the pattern ViroHEX is present in the 5th column of A.txt.

In order to do that I start something like :

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

But I didn't figure it out the part with A.txt. Do you have an idea how to do that?

Best Answer

awk -F'\t' '
  NR==FNR{ if ($5=="ViroHEX"){ viro=1 } next }
  viro && $1=="Software Version"{ $2="VIRO_v1" }
  1
' A.txt FS=" = " OFS=" = " B.txt > result.txt

This replaces the second field (NOVA_v1) with VIRO_v1 in the second file if the first field equals Software Version and ViroHEX is present anywhere in the 5th column of the first file.

I'm assuming the field separator of the second file is <space>=<space> (not a tab).

Related Question