extend awk script for multiple lines in both text files and not if both files have just a single record

Viewed 62

I CAN generate this 3.txt output

AA;BB;CC;DD;

from 2 text files. Both files only have a single record

1.txt

AA;BB;

2.txt

CC;DD;BB;AA;

I use this awk script.

BEGIN{
  FS=OFS=";"
}
{
  for(i=1;i<=NF;i++){
    if(!seen[$i]++){ val=(val?val OFS:"")$i }
  }
  print val
  delete seen
  val=""
}

I use in this way, I use paste command to combine both the files and then processing its output by awk command.

paste 1st.txt 2nd.txt | awk -f tst.awk > "3.txt"

But what I want?
I need to consider more of one single record. I try to adapt this script for this situation. You can see that now both files have not only a single records.

1.txt

UU;    
AA;BB;
ZZ;KK;

2.txt

CC;DD;BB;AA;
LL;KK;
GH;ZZ;SS;

output 3.txt that I try to generate is this

UU;
AA;BB;CC;DD;
ZZ;KK;LL;GH;SS;

processing logic:

Code above just uses literal strings in a hash lookup of array indices so it doesn't care what characters you have in your input.
However about sample output: if in 2.txt there are common fields also in 1.txt.for example BB;AA; then you need concatenate them in a single row, i.e AA;BB;CC;DD; Ordering is not required, for example is not relevant if output is BB;AA;DD;CC; Only condition that is required is avoid duplicates fields in the same row

When I say 'avoid duplicates' I mean avoid this output

AA;BB;CC;DD;BB;AA;

because output should be

AA;BB;CC;DD;
1 Answers

Maybe this is what you want:

BEGIN{FS=OFS=";"}
{sub(/;$/,"")}
NR==FNR{
    for (i=1;i<=NF;++i)
        for (j=1;j<=NF;++j)
            if (j!=i&&!b[$i,$j]++)
                a[$i,++c[$i]]=$j
    next
}
{
    delete b
    for (i=1;i<=NF;++i)
        ++b[$i]
    for (i=1;i<=NF;++i)
        for (j=1;j<=c[$i];++j)
            if (!b[a[$i,j]]++)
                $(NF+1)=a[$i,j]
    print
}

Use it like this:

awk -f script.awk 2.txt 1.txt

Here are some previous questions (presumably yours - you might want to look over them and work on how to ask your question properly):

Related