Comparing Columns of two FIles


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing Columns of two FIles
# 1  
Old 02-16-2007
Comparing Columns of two FIles

Dear all,

I have two files in UNIX File1 and File2 as below:

File1:
1,1234,.,67.897,,0
1,4134,.,87.97,,4
0,1564,.,97.8,,1

File2:
2,8798,.,67.897,,0
2,8879,.,77.97,,4
0,1564,.,97.8,,1

I want to do the following:
(1) Make sure that both the files have equal number of columns and if not error out
(2) If there are equal number of columns then compare each corresponding field from both the files line by line and if there is any difference then print the two different values with line number and column number.

I know that 'diff -w' is useful here because it compares ignoring leading, trailing or in-between spaces which is useful because all my columns are numeric.

Can I use say 'awk' and its NF and NR variables along with diff? Or is there any other way? Please advise. I thank you for your help.

GG
# 2  
Old 02-18-2007
Wil all lines in a file have same number of columns?
# 3  
Old 02-18-2007
Yes. All lines have the same number of columsn, it is like a matrix. Thanks.
# 4  
Old 02-18-2007
here's something to start with:

nawk -F',' -f gg.awk File1 File2

gg.awk:
Code:
FNR==NR{
  arr[FNR]=$0
  next;
}
{
  f1num=split(arr[FNR], f1A, FS)
  if ( f1num != NF ) {
     printf("error: [%d] - unequal number of fields\n", FNR)
     next
  }
  for(i=1; i<=NF; i++)
    if (f1A[i] != $i)
     printf("error: [%d]::[%d] - non-equal fields: [%d != %d]\n", FNR, i, f1A[i]
, $i)
  split("", f1A)
}

# 5  
Old 02-21-2007
The next step

I thank you so much for your reply. It seems to work but I wish to expand the functionality a little further,.by printing labels in the output along with all the currently displaying fields, i.e:

File1:
1,1234,.,67.897,,0
1,4134,.,87.97,,4
0,1564,.,97.8,,1

File2:
2,8798,.,67.897,,0
2,8879,.,77.97,,4
0,1564,.,97.8,,1

File3:
Label1, Label2, ...LabelN

Where File1 and File2 are same as the old files and file3 now contains just one row with labels corresponding to the fields (which are the same labels for both file1 and file2). Can I now display the same output as is generated by your code (gg.awk) except I also want to print these corresponding labels on the far right side when the fields are different so that the output now looks like:

error: [1]::[1] - non-equal fields: [1 != 2] "label1"

error: [1]::[2] - non-equal fields: [1234 != 8798] "label2"

etc...

with the labels coming from file3.

Also, can you also offer some explanation as to what this code is doing. I got a rough idea while exploring it but still some features seem a little advanced to me. Any information would be very useful.

I thank you immensely for all this help,

GG
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Need help in comparing multiple columns from two files.

Hi all, I have two files as below. I need to compare field 2 of file 1 against field 1 of file 2 and field 5 of file 1 against filed 2 of file 2. If both matches , then create a result file 1 with first file data and if not matches , then create file with first fie data. Please help me in... (12 Replies)
Discussion started by: sivarajb
12 Replies

2. Shell Programming and Scripting

Comparing columns in two separate files

Hey all, I have a file structure that looks something like this: file1 306708278 88954535 234167885 file2 2012-03-27T12:32:56+00:00 137 Orchotorena 184616310003601409 306708278 es 40.4777947 Majadahonda -3.6416896333333333 0 false atlante83 "<a href=""http://tapbots.com/tweetbot""... (8 Replies)
Discussion started by: dgaff
8 Replies

3. Shell Programming and Scripting

Comparing two columns from two different files

Hi, I have a single-column file1 having records like: 00AB01/11 43TG22/00 78RC09/34 ...... ...... and a second file , file 2 having two columns like 78RC09/34 1 45FD11/11 2 00AB01/11 3 43TG22/00 4 ...... ...... (8 Replies)
Discussion started by: amarn
8 Replies

4. Shell Programming and Scripting

AWK: Comparing two columns from two different files

Hi - I have two files as follows: File 1: chr5 118464905 118465027 ENST00000514151 utr5 0 + chr5 118464903 118465118 ENST00000504031 utr5 0 + chr5 118468826 118469180 ENST00000504031 utr5 0 + chr5 118469920 118470084 ... (14 Replies)
Discussion started by: polsum
14 Replies

5. Shell Programming and Scripting

comparing two columns from two different files

Hello, I have two files as 1.txt and 2.txt with number as columns. 1.txt 0 53.7988 1 -30.0859 2 20.1632 3 14.2135 4 14.6366 5 -37.6258 . . . 31608 -8.57333 31609 -2.58554 31610 -24.2857 2.txt (1 Reply)
Discussion started by: AKD
1 Replies

6. UNIX for Dummies Questions & Answers

Comparing columns in two files

Hi, I have two files. File1.txt has 2 columns and looks like: 458739 122345 4456 122657 34200 122600 File2.txt has many columns with column 1 the same as column2 of File1.txt, but with lot more rows: 122786 abcdefg user1@email 122778 uuhjeufh user2@email... (1 Reply)
Discussion started by: ursaan
1 Replies

7. UNIX for Dummies Questions & Answers

Comparing 2 columns from 2 files

Hi, I have two files with the same number of columns. Basically I want to print the 2 columns that match between the two files. File1 looks like this: dr12 12 6 abn dr14 12 7 abn File2 looks something like this: dr12 12 8 abn dr12 14 7 abn So basically if the first... (1 Reply)
Discussion started by: kylle345
1 Replies

8. Shell Programming and Scripting

comparing 2 columns from 2 files

Hey, I have 2 files that have a name and then a number: File 1: dog 21 dog 24 cat 33 cat 27 dog 76 cat 65 File 2: dog 109 dog 248 cat 323 cat 207 cat 66 (2 Replies)
Discussion started by: dcfargo
2 Replies

9. Shell Programming and Scripting

comparing the columns in two files

I have two files file1 and file 2 both are having multiple coloumns.i want to select only two columns. i used following code to get the desired columns,with ',' as delimiter cut -d ',' -f 1,2 file1 | sort > file1.new cut -d ',' -f 1,2 file2 | sort > file2.new I want to get the coloums... (1 Reply)
Discussion started by: bab123
1 Replies

10. UNIX for Advanced & Expert Users

Comparing Columns of two FIles

Dear all, I have two files in UNIX File1 and File2 as below: File1: 1,1234,.,67.897,,0 1,4134,.,87.97,,4 0,1564,.,97.8,,1 File2: 2,8798,.,67.897,,0 2,8879,.,77.97,,4 0,1564,.,97.8,,1 I want to do the following: (1) Make sure that both the files have equal number of columns and if... (1 Reply)
Discussion started by: ggopal
1 Replies
Login or Register to Ask a Question