user2043380 user2043380 - 1 month ago 8
Linux Question

AWK MATCHING VALUES OF FIRST TWO COLUMNS and printing in blank field

I have a csv file which looks like below:

2212,A1,
2212,A1,128
2307,B1,
2307,B1,107


how can i copy value of 3rd column in place of missing values in 3rd column of if value of first 2 column is same. e.g. first two columns of first two rows are same so automatically it should print value of 3rd column of second row in missing place of third column of first row.

expected output:

2212,A1,128
2212,A1,128
2307,B1,107
2307,B1,107


Please help as i couldn't even think of a solution and there are millions of values such like this in my file..

Answer

If you first sort the file in reverse order, the rows with data preceed the empty rows:

$ sort -r file
2307,B1,107
2307,B1,
2212,A1,128
2212,A1,

Then use following awk to process the output of sort:

$ sort -r file | awk 'NR>1 && match(prev,$0) {$0=prev} {prev=$0} 1'
2307,B1,107
2307,B1,107
2212,A1,128
2212,A1,128
Comments