Print Missing Header Name from File.

HI Guys,

I have file A.txt

ID,L1,L2,L3,L4
1A,2a,33a,44b,55c
2A,10a,14a,15b,16c

File B.txt

ID
L1
L4
L5

Output:-

ID,L1,L4,L5
1A,55c,n/a
2A,16c,n/a

I have below solution which is works perfect if all header match from file b.

nawk -v OFS="," '$1=$1' $Lic1 > $Lic2

	nawk '
	BEGIN           {OFS = FS = ","}
	FNR == NR       {h[$0] = NR
                 MX = NR
                 next
                }
	FNR == 1        {for (i=1; i<=NF; i++) if ($i in h) a[h[$i]] = i
                }

                {for (i=1; i<=MX; i++) printf "%s%s", $a, (i==MX)?RS:OFS
                }
	' fileB fileA

Thanks

Are you looking to determine the entries not specified?
return L2 and L3 ??

Your output sample seems to miss the L1 column values. However, try

awk '
BEGIN           {OFS    = FS = ","}
FNR == NR       {H[$0]  = NR
                 HI[NR] = $0
                 MX     = NR
                 next
                }
FNR == 1        {for (i=1; i<=NF; i++) if ($i in H) A[H[$i]] = i
                }

                {for (i=1; i<=MX; i++) printf "%s%s",  (A?$A:(FNR==1)?HI:"n/a"), (i==MX)?RS:OFS
                }
' file2 file1
1 Like