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
# 8  
Old 09-10-2010
PM

I tried this one and it returns
I have tried this but it does not work on actual code. I have sent you one line of real data in your private message and this code fails even if you make both the files same.

Quote:
Originally Posted by durden_tyler
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
# 9  
Old 09-10-2010
Post your real data over here.

tyler_durden
# 10  
Old 09-10-2010
Data

Even the files are same but code shows that there is a difference (it is tab delimited):

filea

Code:
Mechanical.Markdown.Directed.POS.$ WK17 10.5

fileb

Code:
Mechanical.Markdown.Directed.POS.$ WK17 10.5

Code:
awk -F \t 'NR==FNR {x[NR]=$0} NR!=FNR {split(x[FNR],a,"\t"); if(int(a[3]) != int($3)) {printf("ROW %d\n< %s\n",FNR,x[FNR],$0)} }' filea fileb




Quote:
Originally Posted by durden_tyler
Post your real data over here.

tyler_durden

Last edited by Scott; 09-10-2010 at 10:51 PM.. Reason: Code tags, please...
# 11  
Old 09-11-2010
Quote:
Originally Posted by yale_work
Even the files are same but code shows that there is a difference (it is tab delimited):
...
Nope, it doesn't. Check this out -

Code:
$ 
$ 
$ cat filea
Mechanical.Markdown.Directed.POS.$    WK17    10.5
$ 
$ cat fileb
Mechanical.Markdown.Directed.POS.$    WK17    10.5
$ 
$ ## show the contents of these files with ^I for TAB characters and $ for end-of-line
$ cat -et filea
Mechanical.Markdown.Directed.POS.$^IWK17^I10.5$
$ 
$ cat -et fileb
Mechanical.Markdown.Directed.POS.$^IWK17^I10.5$
$ 
$ 
$ ## now try the awk script, tweaked a little bit so that it displays a message for lines that match
$ awk -F"\t" 'NR==FNR {x[NR]=$0}
              NR!=FNR {split(x[FNR],a,"\t");
                       if(int(a[3]) != int($3)) {printf("ROW %d\n< %s\n---\n> %s\n",FNR,x[FNR],$0)}
                       else {print "ROW ",FNR,"is the same in both files"}
                      }' filea fileb
ROW  1 is the same in both files
$ 
$ 
$ 
$ ## now try the other case - edit one file so that the last field is different
$ 
$ sed 's/10.5/11.5/' filea >tmp && mv tmp filea
$ 
$ ## check the contents of both files again
$ cat filea
Mechanical.Markdown.Directed.POS.$    WK17    11.5
$ 
$ cat fileb
Mechanical.Markdown.Directed.POS.$    WK17    10.5
$ 
$ ## finally, try the awk script once again
$ awk -F"\t" 'NR==FNR {x[NR]=$0}
              NR!=FNR {split(x[FNR],a,"\t");
                       if(int(a[3]) != int($3)) {printf("ROW %d\n< %s\n---\n> %s\n",FNR,x[FNR],$0)}
                       else {print "ROW ",FNR,"is the same in both files"}
                      }' filea fileb
ROW 1
< Mechanical.Markdown.Directed.POS.$    WK17    11.5
---
> Mechanical.Markdown.Directed.POS.$    WK17    10.5
$ 
$ 

If your results are different, then my best guess is that either one or both the files aren't truly tab-delimited.
Check the octal dump of each file to see what exactly is in there.

Code:
od -bc filea
od -bc fileb

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 12  
Old 09-11-2010
Quote:
Originally Posted by yale_work

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.
With new input:

Code:
awk -F, '
{split($3,x,".");a=$1 FS $2 FS x[1] ;b=$0}
{getline < "fileb" ; split ($3,y,".");}
{if (a!=$1 FS $2 FS y[1]) print b "|" $0}
' filea

X3.T3,Y3.T4,4.03|X3.T3,Y3.T4,5.03

This User Gave Thanks to rdcwayx For This Post:
# 13  
Old 09-13-2010
Thanks durden_tyler and rdcwayx.
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