How to replace a string in one file if a pattern present in another file using awk

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).


A simpler solution, in my humble opinion. Unfortunately it uses other tools besides awk.

awk '{print $5}' A.txt | grep -q VIROHEX && sed 2s/NOVA/VIRO/ B.txt

The part before && implements the condition, the sed program replaces the text in file B.txt.

EDIT: Thanks, AdminBee, for pointing out that my solution is lazy. Better:

awk '{print $5}' A.txt | grep -q VIROHEX && sed '2s/=.*/= VIRO_v1/' B.txt

Similar to berndbausch's solution, but being a bit more careful about what we're actually matching and inserting.

awk -F '\t' '$5 == "ViroHEX" { found = 1; exit } END { exit !found }' A.txt &&
sed '2 s/=.*/= VIRO_v1/' B.txt

This first uses awk to determine whether the exact string ViroHEX occurs in the fifth tab-delimited field, on any line, in the file A.txt. It does not read more of the file than what is necessary, and exits with an exit status that is later used to conditionally run sed.

The sed command replaces everything form the first = character on the second line of B.txt with the string = VIRO_v1.

If the A.txt file does not contain ViroHEX in the fifth column, no output is produced.

The following variation always produces the B.txt file, possibly with a replacement done on the second line:

if awk -F '\t' '$5 == "ViroHEX" { found = 1; exit } END { exit !found }' A.txt
then
    sed '2 s/=.*/= VIRO_v1/'
else
    cat
fi <B.txt

If you, instead of modifying line 2, want to modify the line saying Software Version = ..., then change the expression used in the calls to sed into

s/^\(Software Version = \).*/\1 VIRO_v1/