Search code examples
awkgawk

comparison of two file & mark changed fields.


this is a extended question from awk-to-compare-two-file-by-identifier-output-in-a-specific-format

As specified let's say two file have some difference

file 1

a||d||f||a
1||2||||4


file 2

a||d||f||a
1||1||3||4
1||2||r||f

where the desired output will be

1||1#2||3#||4
1||2||r||f

where you can see i only want to compare the files & print accordingly such as if any update found in file 2 will be printed as updated_value#oldvalue & any new line added to file 2 will also be updated accordingly.

& i want to output the difference now as @fedorqui suggested

awk 'BEGIN{FS=OFS="|"}
     FNR==NR {for (i=1;i<=NF;i+=2) a[FNR,i]=$i; next}
     {for (i=1; i<=NF; i+=2)
         if (a[FNR,i] && a[FNR,i]!=$i)
             $i=$i"#"a[FNR,i]
     }1' f1 f2

which results

1||1#2||3||4
1||2||r||f

this works pretty well but it's unable to handle the blank lines as changed requirements. can anyone help me?


Solution

  • This command

    awk 'BEGIN{FS=OFS="|"}
         NR==FNR{for(i=1;i<=NF;i++)a[FNR" "i]=$i;b[FNR]=$0;next}
         {for(i=1;i<=NF;i++)(FNR" "i in a)&&a[FNR" "i]!=$i&&$i=$i"#"a[FNR" "i]}
         !(b[FNR]==$0)' file{1,2}
    

    produces

    1||1#2||3#||4
    1||2||r||f
    

    Which is the expected output.