Results 1 to 8 of 8
Hi all
I have two text files, say for example
test1.txt contains the following
20,11,1998,00,00,00,324.000000,-9978,1111
22,11,1998,00,00,00,326.000000,-9865,2222
23,11,1998,00,00,00,327.000000,-9999,3333
27,11,1998,00,00,00,331.000000,-9865,4444
test2.txt contains
19,11,1998,11,00,00,00,324.000000,-9444,5555
20,11,1998,22,00,00,00,326.000000,-9999,6666
21,11,1998,33,00,00,00,327.000000,-9223,7777
22,11,1998,44,00,00,00,331.000000,-9999,8888
The first 3 comma separated values ...
- 10-17-2007 #1Just Joined!
- Join Date
- Oct 2007
- Posts
- 6
Script to compare to files
Hi all
I have two text files, say for example
test1.txt contains the following
20,11,1998,00,00,00,324.000000,-9978,1111
22,11,1998,00,00,00,326.000000,-9865,2222
23,11,1998,00,00,00,327.000000,-9999,3333
27,11,1998,00,00,00,331.000000,-9865,4444
test2.txt contains
19,11,1998,11,00,00,00,324.000000,-9444,5555
20,11,1998,22,00,00,00,326.000000,-9999,6666
21,11,1998,33,00,00,00,327.000000,-9223,7777
22,11,1998,44,00,00,00,331.000000,-9999,8888
The first 3 comma separated values shows the date.
I want to check for the similar date in both the files and return the whole data into another file.
Need the o/p file like this
20,11,1998,00,00,00,324.000000,-9978,1111,22,00,00,00,326.000000,-9999,6666
22,11,1998,00,00,00,326.000000,-9865,2222,44,00,00,00,331.000000,-9999,8888
Guess I made the thread clear to understand
Can any one help me out please......
- 10-17-2007 #2
What about the command cmp

But to do this in a script you do it like this:
Pseudo Mode On
Open File1
Open File2
Do Untill EOF (File1 && File2)
{
Read Line File1 -> into char* Line
Read Line File2 -> into char* Line2
if (Line != Line2){show that its diffrent}
}
Close files..
If you need help with the real script I can write it in a couple of languages.
Perl and C++ prefer.
Edit: I just saw that you also need with a Date.
Then I'd pick Perl for this task.
The perl index Function or split are quite powerfull.
Perl is real good for Text Moding.
- 10-17-2007 #3
Is this a homework (see this)
In your case it should be:
Code:awk 'NR==FNR{x[$1$2$3]=$0;next} $1$2$3 in x{p=x[$1$2$3]"," sub(/[0-9]*,[0-9]*,[0-9]*,/,"") print p$0 }' FS="," test1.txt test2.txt
- 10-18-2007 #4Just Joined!
- Join Date
- Oct 2007
- Posts
- 6
Thank you radoulov! U r really great!!! U really saved my soul!!! It really worked. Thanx a lot!
- 10-29-2007 #5Just Joined!
- Join Date
- Oct 2007
- Posts
- 6
This script is matching these two as same! Yet its diffrent
31,1,1998 and 3,11,1998
some one can fix it plz....
- 10-29-2007 #6
- 10-29-2007 #7Code:
awk 'NR == FNR { x[$1SUBSEP$2SUBSEP$3] = $0; next } $1SUBSEP$2SUBSEP$3 in x { p = x[$1SUBSEP$2SUBSEP$3] sub( /[0-9]*,[0-9]*,[0-9]*/, "" ) print p $0 }' FS="," test1.txt test2.txt
- 10-30-2007 #8Just Joined!
- Join Date
- Oct 2007
- Posts
- 6
Thanx frndz! Now i think am clear with some of the basics of awk! Thanx to radoulov and Robin


Reply With Quote