-1

I want to compare two files and get the difference in new file with the changed column number for each row. Both the files have same structure with sorted data and same number of rows.

I have two files,

File 1

01|Akshaya|9657|Pune
02|Samiksha|8391|Mumbai

File 2

01|Akshaya|9657|Nashik
02|Samiksha|0202|Nagpur

I want to compare these two files and print details of difference of records. like for 1st row for which column is having mismatch. so when we compare above two files first row in 2nd file is not same as first row in 1st file . so output is like below.

File 3

Sr no|Col no
01|3
02|2,3

closed as unclear what you're asking by Jeff Schaller, jimmij, Mr Shunz, Rui F Ribeiro, Christopher Feb 19 at 16:20

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Most likely not, because your process of getting from the input to the output is totally unclear. Please edit the question and explain more what you're trying to do. A larger set of data would help aswell. – Panki Feb 19 at 7:57
  • This reads as an assignment, which is not the concept of this site. What did you try, where did it fail? – Jacob Vlijm Feb 19 at 8:23
1

Put the same line numbers of each file side-by-side and compare the columns:

$ paste -d"|" file_1 file_2 \
  | awk -v FS="|" 'BEGIN {print "Sr no|Col no"} \
    { printf $1"|"; \
      for(i=2;i<=4;i++) { \
       if($i!=$(i+4)) printf i-1","; \
      } \
      print "";
    }'

Note that awk starts counting the columns by 1. Your example looks like it starts with 0. That's why I subtract 1 in the output.

Also note that there will be trailing , at each line. If necessary you can remove it by piping to sed:

| sed 's/,$//'

Not the answer you're looking for? Browse other questions tagged or ask your own question.