2

I need to compare two files, File1.csv and File2.csv (Separated by ',') using Awk

Logic:

Column PID in both files are reference.

If for same 'PID' in 'File1.csv' and 'File2.csv' and in both files column 'Value' value is same then 'File2.csv' column 'UTS' value update for this column getting value from 'File1.csv' column 'RTS'.

Else for same 'PID' in 'File1.csv' and 'File2.csv' and in both files column 'Value' value is not same then 'File2.csv' column 'Value,RTS,UTS' value update for this column getting value from 'File1.csv' column 'Value,RTS,UTS'

File1.csv:

PID,Value,RTS
1,50,10:30:00
2,22,10:15:00
3,34,10:18:00
4,54,10:20:00
5,54,10:22:00
6,54,10:25:00
7,80,10:50:00
8,60,10:32:00
9,45,10:35:00

File2.csv:

PID,Value,RTS,UTS
1,12,10:10:00,10:14:00
2,22,10:15:00,10:17:00
3,34,10:18:00,10:19:00
4,54,10:20:00,10:21:00
5,54,10:22:00,10:24:00
6,54,10:25:00,10:29:00
7,60,10:30:00,10:31:00
8,60,10:32:00,10:34:00
9,45,10:35:00,10:35:00

Output:

PID,Value,RTS,UTS
1,50,10:30:00,10:30:00
2,22,10:15:00,10:15:00
3,34,10:18:00,10:18:00
4,54,10:20:00,10:20:00
5,54,10:22:00,10:22:00
6,54,10:25:00,10:25:00
7,80,10:50:00,10:50:00
8,60,10:32:00,10:32:00
9,45,10:35:00,10:35:00
0

1 Answer 1

2

Try this:

$ awk -F',' '
    FNR == NR {
        if (FNR == 1) {next}
        a[$1] = $2;
        b[$1] = $3;
        next;
    }
    {
        if (FNR == 1) {print;next}
        if (a[$1] == $2) {
            print $1,$2,$3,b[$1];
        }
        else {
            print $1,a[$1],b[$1],b[$1];
        }
    }
  ' OFS=',' file1,file2
PID,Value,RTS,UTS
1,50,10:30:00,10:30:00
2,22,10:15:00,10:15:00
3,34,10:18:00,10:18:00
4,54,10:20:00,10:20:00
5,54,10:22:00,10:22:00
6,54,10:25:00,10:25:00
7,80,10:50:00,10:50:00
8,60,10:32:00,10:32:00
9,45,10:35:00,10:35:00
1
  • Do you set FS=, in script.awk? Try running it from command line.
    – cuonglm
    Commented May 30, 2014 at 10:19

You must log in to answer this question.

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