String Comparison between two files using awk

I have two files with field seperator as "~".
File A: 12~13~14~15
File B: 22~22~32~11

i want to calculate the difference between two files and than calculate the percentage difference and output it to a new file.
How do i do this using awk.

Also please suggest GOOD awk tutorials.

Thank you

Of which fields do you want to calculate the difference? Which fields are you comparing between the two files?

I want to get difference of each column from each file
example
File A: column 1 - File B: column 1
File A: column 2 - File B: column 2
and so on

Quick and dirty, and I made a lot of assumptions. This outputs the difference and percentage difference between corresponding fields in fileA.txt and fileB.txt. Output format for each pair of fields is difference/percentage --

cat fileA.txt:

12~13~14~15
16~83~10~44
75~84~96~56

cat fileB.txt:

22~22~32~11
73~85~52~43
74~51~14~68
paste file[AB].txt | sed 's/	/~/g' | \
awk '
  BEGIN { FS="~" }
  {
    divs=NF/2
    for ( i = 1; i <= divs; i++ ) {
      f1=i
      f2=i+divs
      diff=$f1-$f2
      pct=diff/$f1
      printf("%.0f/%.2f ",diff,pct)
    }
    printf("\n","")
  }'

Yields:

-10/-0.83 -9/-0.69 -18/-1.29 4/0.27
-57/-3.56 -2/-0.02 -42/-4.20 1/0.02
1/0.01 33/0.39 82/0.85 -12/-0.21

Great, this is what i wanted to see as output..... :slight_smile:

Glenn,

Can you pls explain the above code to me, I tried to understand it, but couldn't figure it out, your reply would be highly appreciated. Thanks.

Cheers,
Patras

Sure thing...

fileA.txt contains:

12~13~14~15
16~83~10~44
75~84~96~56

fileB.txt contains:

22~22~32~11
73~85~52~43
74~51~14~68

The code:

paste file[AB].txt | sed 's/	/~/g' | \
awk '
  BEGIN { FS="~" }
  {
    divs=NF/2
    for ( i = 1; i <= divs; i++ ) {
      f1=i
      f2=i+divs
      diff=$f1-$f2
      pct=diff/$f1
      printf("%.0f/%.2f ",diff,pct)
    }
    printf("\n","")
}'

Paste files A and B together, records side by side (the whitespace between them is a tab):

paste file[AB].txt
12~13~14~15     22~22~32~11
16~83~10~44     73~85~52~43
75~84~96~56     74~51~14~68

Replace tabs with tildes (~), so we have one record pair per line:

sed 's/	/~/g'
12~13~14~15~22~22~32~11
16~83~10~44~73~85~52~43
75~84~96~56~74~51~14~68

...which we pipe through awk:
Set the field separator to ~

awk '
  BEGIN { FS="~" }

We're assuming that both files contain the same number of records, and that each
record pair in the files contains an equal number of fields. Therefore, we want
to split the whole record, e.g. "12~13~14~15~22~22~32~11" into two parts. So we
set divs equal to half the number of fields:

  {
    divs=NF/2

In this case, each file has four fields, so we are going to find the differences
of fields 1 and 5, 2 and 6, 3 and 7, and 4 and 8. The "divs=NF/2" method just
lets us determine at run time how many fields we are dealing with:

Field #:  1  2  3  4  5  6  7  8
Record:  12~13~14~15~22~22~32~11

For each joined record pair, we want to perform as many calculations as there are
record pairs. The number of record pairs is determined dynamically for each line:

    for ( i = 1; i <= divs; i++ ) {

Set f1 equal to our iterator. This will be the fileA.txt value for that field number.
It gets incremented after each comparison:

      f1=i

Set f2 equal to the iterator plus the number of fields in each div, e.g. if i is field 1,
then we want f2 to be field 5. So f2=i+divs --> f2=1+4.

      f2=i+divs

Calculate the difference between field1 and field2. This is tricky because we are not
subtracting the value of f2 from the value of f1; f1 and f2 are pointers (i.e. field numbers).
So $f1 is $1 and $f2 is $5, then on the next iteration $f1 is $2 and $f2 is $6, and so on:

      diff=$f1-$f2

Same concept -- just take the difference and divide by the fileA value:

      pct=diff/$f1

Print output of diff and pct, using formats %.0f and %.2f respectively (this is arbitrary; I
just chose those formats for looks).

      printf("%.0f/%.2f ",diff,pct)

After the loop is finished for the record pair, print a new line:

    }
    printf("\n","")
}'

All done!

Hi Glenn,

Thanks buddy, that was more than I expected, I think, its almost impossible to find more helpful UNIX stuff than these forums on this planet.

Cheers,
Patras