Template A.tsv:

Name    data

And several files to analyse, for example, B.txt:

16  LSD

and C.txt:

16  ARH

I want to add OK or KO to A.tsv based on whether LSD is present in the other files:

Name    data
B   OK
C   KO

The field separators are tabs (\t) for all files. How to do that with awk?

I started with FILENAME recognition but no clue for the rest:

for bla in data/*.txt ; do
r="$(basename -s ".txt" $bla)"
( head -n 1 $template
awk -F'\t' -v OFS="\t" -v filename=$r  [..........] >> output_final.tsv;

1 Answer 1

awk '
  !last && /LSD/{seen[FILENAME]=1; nextfile;}
    if (FNR<2) print
    else print $1 "\t" ( seen[$1".txt"] ? "OK" : "KO" )
' *.txt last=1 A.tsv

First Awk reads the txt files, for which last is unset, and looks for a LSD match. If found, it puts the filename in the seen array (nextfile is just an optimization). When it reaches the A.tsv file last is set, so it prints its header and checks for each remaining line if the corresponding filename is in seen, adding <tab>OK if yes and <tab>KO if not.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .