Compare two files based on integer part only


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare two files based on integer part only
# 1  
Old 09-09-2010
Compare two files based on integer part only

Please see how can I do this:

File A (three columns):
Code:
X1,Y1,1.01
X2,Y2,2.02
X3,Y3,4.03


File B (three columns):
Code:
X1,Y1,1
X2,Y2,2
X3,Y3,4.0005

Now I have to compare file A and B based on the integer part of column 3. Means first 2 rows should be OK and the third row should not satisfy the criteria. First two columns make a unique row in one file so no row will be repeated in a file. Same first two columns will be in both the files....means if we can build a logic to compare the integer part of third column for each row (based on column 1 and 2). Thanks.

Last edited by Franklin52; 09-10-2010 at 04:04 AM.. Reason: Please use code tags!
# 2  
Old 09-09-2010
Quote:
Originally Posted by yale_work
...

File A (three columns):
Code:
X1,Y1,1.01
X2,Y2,2.02
X3,Y3,4.03

File B (three columns):
Code:
X1,Y1,1
X2,Y2,2
X3,Y3,4.0005

Now I have to compare file A and B based on the integer part of column 3. Means first 2 rows should be OK and the third row should not satisfy the criteria. ...
Why not ?
Integer part of row 3, column 3 in file A = int(4.03) = 4
Integer part of row 3, column 3 in file B = int(4.0005) = 4

So as per your logic, row nos. 3 in both files should be considered a match.

tyler_durden
# 3  
Old 09-09-2010
Sorry

Sorry. Yes you are right. Please consider the layout of files as follows:
File A (three columns):
Code:
X1,Y1,1.01
X2,Y2,2.02
X3,Y3,4.03


File B (three columns):
Code:
X1,Y1,1
X2,Y2,2
X3,Y3,5.0005


Quote:
Originally Posted by durden_tyler
Why not ?
Integer part of row 3, column 3 in file A = int(4.03) = 4
Integer part of row 3, column 3 in file B = int(4.0005) = 4

So as per your logic, row nos. 3 in both files should be considered a match.

tyler_durden

Last edited by Franklin52; 09-10-2010 at 04:05 AM.. Reason: Please use code tags, thank you!
# 4  
Old 09-10-2010
Here's an idea -

Code:
$ 
$ 
$ cat filea
x1,y1,1.01
x2,y2,2.02
x3,y3,4.03
x4,y4,7.0001
x5,y5,9.9997
$ 
$ 
$ cat fileb
x1,y1,1
x2,y2,2
x3,y3,5.0005
x4,y4,7.9998
x5,y5,4.0003
$ 
$ 
$ awk -F, 'NR==FNR {x[NR]=$0}
           NR!=FNR {split(x[FNR],a,",");
                    if(int(a[3]) != int($3)) {printf("ROW %d\n< %s\n---\n> %s\n",FNR,x[FNR],$0)}
                   }' filea fileb
ROW 3
< x3,y3,4.03
---
> x3,y3,5.0005
ROW 5
< x5,y5,9.9997
---
> x5,y5,4.0003
$ 
$ 
$ 

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 5  
Old 09-10-2010
Code:
awk -F \. '{a=$1;b=$0 ;getline< "fileb"}{if ($1!=a)print b "|" $0}' filea

This User Gave Thanks to rdcwayx For This Post:
# 6  
Old 09-10-2010
..............

---------- Post updated at 01:25 PM ---------- Previous update was at 01:24 PM ----------

Following suggested command is getting integer part based on the decimal(.):
Code:
awk -F \. '{a=$1;b=$0 ;getline< "fileb"}{if ($1!=a)print b "|" $0}' filea

Actually in my case column 1 and column 2 also have dot(.) so this command is not returning correct values. I have to compare on column 3 only. My files are as follows:

filea
Code:
X1.T1,Y1,1.01
X2,Y2.T2,2.02
X3.T3,Y3.T4,4.03

fileb
Code:
X1.T1,Y1,1
X2,Y2.T2,2
X3.T3,Y3.T4,5.03

Need to compare integer value of column 3 only.


Quote:
Originally Posted by rdcwayx
Code:
awk -F \. '{a=$1;b=$0 ;getline< "fileb"}{if ($1!=a)print b "|" $0}' filea


Last edited by Scott; 09-10-2010 at 10:49 PM.. Reason: Please use code tags
# 7  
Old 09-10-2010
Code:
$
$
$ cat filea
X1.T1,Y1,1.01
X2,Y2.T2,2.02
X3.T3,Y3.T4,4.03
$
$ cat fileb
X1.T1,Y1,1
X2,Y2.T2,2
X3.T3,Y3.T4,5.03
$
$
$ awk -F, 'NR==FNR {x[NR]=$0}
           NR!=FNR {split(x[FNR],a,",");
                    if(int(a[3]) != int($3)) {printf("ROW %d\n< %s\n---\n> %s\n",FNR,x[FNR],$0)}
                   }' filea fileb
ROW 3
< X3.T3,Y3.T4,4.03
---
> X3.T3,Y3.T4,5.03
$
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare two files based on column

Hi, I have two files roughly 1200 fields in length for each row, sorted on the 2nd field. I need to compare based on that 2nd column between file1 and file2 and print lines that exist in both files into separate files (I can't guarantee that every line in file1 is in file2). Example: File1: ... (1 Reply)
Discussion started by: origon
1 Replies

2. Shell Programming and Scripting

A simpler way to do this (save a list of files based on part of their name)

Hello, I have a script that checks every file with a specific extension in a specific directory. The file names contain some numerical output and I am recording the file names with the best n outcomes. The script finds all files in the directory with the extension .out.txt and uses awk to... (12 Replies)
Discussion started by: LMHmedchem
12 Replies

3. Shell Programming and Scripting

Compare files in a folder based on another file

I have a file named file.txt that looks as follows //class1.txt 45 234 67 89 90 //class2.txt 456 34 78 89 120 class1 and class2.txt are the names of files in a folder named folder1. The content of class1.txt file in folder1 67 9 89 5 234 9The content of class2.txt file in... (1 Reply)
Discussion started by: jaff rufus
1 Replies

4. Shell Programming and Scripting

Moving files based on size (string to integer)

I have a log file that I want to archive out as it reaches 100MB. I am using the following to get the file size into a variable but get the error "line 5: filesize=$(wc -c < logfile.log) if then echo "is greater than 100M" else echo "is less than 100M" fi I'm sure there's something... (2 Replies)
Discussion started by: Flakman
2 Replies

5. Shell Programming and Scripting

Compare three files based on two fields

Guys, I tried searching on the internet and I couldn't get the answer for this problem. I have 3 files. First 2 fields of all of them are of same type, say they come from various databases but first two fields in the 3 files means the same. I need to verify the entries that are not present... (4 Replies)
Discussion started by: PikK45
4 Replies

6. Shell Programming and Scripting

how to compare string integer with an integer?

hi, how to I do this? i="4.000" if ; then echo "smaller" fi how do I convert the "4.000" to 4? Thanks! (4 Replies)
Discussion started by: h0ujun
4 Replies

7. Shell Programming and Scripting

compare 2 files based on columns

Hi Experts, Is there a way to compare 2 files by columns and print matching cases. I have 2 files as below, I want cases where col1 and col2 in f1 matches col1 and col2 in f2 to be printed as output. The separator is space. I want the output to have col1 col2 col 3 from both files printed... (7 Replies)
Discussion started by: novice_man
7 Replies

8. Shell Programming and Scripting

How to compare 2 files & get only few columns based on a condition related to both files?

Hiiiii friends I have 2 files which contains huge data & few lines of it are as shown below File1: b.dat(which has 21 columns) SSR 1976 8 12 13 10 44.00 39.0700 70.7800 7.0 0 0.00 0 2.78 0.00 0.00 0 0.00 2.78 0 NULL ISC 1976 8 12 22 32 37.39 36.2942 70.7338... (6 Replies)
Discussion started by: reva
6 Replies

9. UNIX for Dummies Questions & Answers

Report of duplicate files based on part of the filename

I have the files logged in the file system with names in the format of : filename_ordernumber_date_time eg: file_1_12012007_1101.txt file_2_12022007_1101.txt file_1_12032007_1101.txt I need to find out all the files that are logged multiple times with same order number. In the above eg, I... (1 Reply)
Discussion started by: sudheshnaiyer
1 Replies

10. Shell Programming and Scripting

get integer part

Hi, I did a df|awk| command and it returns a percentage "94%", how could I only get the integer part "94" out of it, so I can compare it to another number, I knwo that I have to pipe it to sth, but "grep " did not work, it still give me number WITH the percentage, does someone know what... (3 Replies)
Discussion started by: ericaworld
3 Replies
Login or Register to Ask a Question