Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DIFF Help

Status
Not open for further replies.

hg4372

Technical User
Feb 9, 2009
43
US
I'm trying to do a diff on two files which works, however some of the strings our out of sync in the output file. Any ideas ?

FILE 1
231 0 1 1 9144 11 113434 545 193
231 0 2 2 1377 0 1392 16 6
231 0 7 1 51 0 338 290 142
231 0 7 2 1433 0 1469 32 12

FILE 2
231 0 1 1 9145 11 113435 545 193
231 0 2 2 1394 0 1409 16 6
231 0 7 1 52 0 339 290 142
231 0 7 2 1441 0 1479 32 12

OUTPUT FILE
231 0 1 1 9144 11 113434 545 193
231 0 1 1 9145 11 113435 545 193
231 0 2 2 1377 0 1392 16 6
231 0 2 2 1394 0 1409 16 6
231 0 7 1 51 0 338 290 142
231 0 7 2 1433 0 1469 32 12
231 0 7 1 52 0 339 290 142
231 0 7 2 1441 0 1479 32 12

Might be hard to see, but the output file has some of the lines out of order. Look at lines 5,6,7,8.

I want it to look like this.

231 0 1 1 9144 11 113434 545 193
231 0 1 1 9145 11 113435 545 193
231 0 2 2 1377 0 1392 16 6
231 0 2 2 1394 0 1409 16 6
231 0 7 1 51 0 338 290 142
231 0 7 1 52 0 339 290 142
231 0 7 2 1433 0 1469 32 12
231 0 7 2 1441 0 1479 32 12

Does anyone know why it would be doing that, and how to correct it ?

Please Help
 
This is the command I'm using inside the KSH script after I pull the 2 files.

diff TEST.yesterday.txt TEST.today.txt > TEST.DIFF.txt
 
Well, any old diff on my unix gives this:

Code:
$ diff file1 file2
1,4c1,4
< 231 0 1 1 9144 11 113434 545 193
< 231 0 2 2 1377 0 1392 16 6
< 231 0 7 1 51 0 338 290 142
< 231 0 7 2 1433 0 1469 32 12
---
> 231 0 1 1 9145 11 113435 545 193
> 231 0 2 2 1394 0 1409 16 6
> 231 0 7 1 52 0 339 290 142
> 231 0 7 2 1441 0 1479 32 12

Are you sure the diff inside that script isn't a shell function or other wrap around diff?


HTH,

p5wizard
 
I didn't think it mattered but maybe it does. Here is what it initially looks like. Then I run it through grep to remove everything but the lines with a | in it.

As you can see even before that the file is out of sync

Diff File
| 231| 1|2|2| 645612| 38| 1000882| 3108| 119
---
| 231| 1|2|2| 647935| 38| 1003368| 3108| 119
37,38c37,38
| 231| 1|2|4| 936| 0| 1340| 593| 281
| 231| 1|3|1| 1331513| 8| 2386082| 2159| 137
---
| 231| 1|2|4| 944| 0| 1354| 601| 284
| 231| 1|3|1| 1347807| 8| 2416500| 2184| 137
 
Here is the entire script that runs every 24 hours. Hope this helps.

#!/usr/bin/ksh
# echo "Enter switch: \c"
# read switch
# echo "Enter username: \c"
# read username
# echo "Enter password: \c"
# read password
switch=$1
dt=$(date +%m%d%y_%H%M)
username=
password=

for x in 1
do
{
echo $username
sleep 5
echo $password
sleep 5
echo "d laps/* sts/* vt1dot5/*,* ds1 erroredSec,sevErroredSec,crcErrors,frmErrors,slip"
sleep 120
echo "quit"

}|telnet $switch>TEST.txt
cp TEST.txt TEST.t1_$dt
rm TEST.yesterday.txt
cp TEST.today.txt TEST.yesterday.txt
rm TEST.today.txt
rm TEST.D.txt
rm /TESTDIR/TEST.DIFF.txt

done

grep "|" TEST.txt > TEST.today.txt
diff TEST.yesterday.txt TEST.today.txt > TEST.D.txt
grep "|" TEST.D.txt > /DIFFDIR/TEST.DIFF.txt
 
| 231| 1|2|2| 645612| 38| 1000882| 3108| 119
---
| 231| 1|2|2| 647935| 38| 1003368| 3108| 11937,38c37,38
| 231| 1|2|4| 936| 0| 1340| 593| 281
| 231| 1|3|1| 1331513| 8| 2386082| 2159| 137
---
| 231| 1|2|4| 944| 0| 1354| 601| 284
| 231| 1|3|1| 1347807| 8| 2416500| 2184| 137

Above | 231| 1|2|4 should have both entries together,instead it goes

| 231| 1|2|4
| 231| 1|3|1
| 231| 1|2|4
| 231| 1|3|1

 
What about this ?
grep "|" TEST.D.txt[!] | sort[/!] > /DIFFDIR/TEST.DIFF.txt

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
| 231| 1|2|2| 645612| 38| 1000882| 3108| 119
---
| 231| 1|2|2| 647935| 38| 1003368| 3108| 119
37,38c37,38
| 231| 1|2|4| 936| 0| 1340| 593| 281
| 231| 1|3|1| 1331513| 8| 2386082| 2159| 137
---
| 231| 1|2|4| 944| 0| 1354| 601| 284
| 231| 1|3|1| 1347807| 8| 2416500| 2184| 137

There's probably another "c" indication before this:
36c36
| 231| 1|2|2| 645612| 38| 1000882| 3108| 119
---
| 231| 1|2|2| 647935| 38| 1003368| 3108| 119
37,38c37,38
| 231| 1|2|4| 936| 0| 1340| 593| 281
| 231| 1|3|1| 1331513| 8| 2386082| 2159| 137
---
| 231| 1|2|4| 944| 0| 1354| 601| 284
| 231| 1|3|1| 1347807| 8| 2416500| 2184| 137

line 36 is changed (then line 36 from file1 and then line 36 from file2 is shown)

lines 37-38 are changed (both lines from file1 and then from file2 are shown)

So what is out of sync? That is just how diff works...


HTH,

p5wizard
 
I didn't realize it worked that way. Thanks for the explaining. Is there a way to make it show the differences together ? Even though 37 & 38 both had differences put line 37 from file1 above the line 37 from file2 ? Instead of file1 37&38, then file2 37&38 ?
 
I did try the grep "|" TEST.D.txt | sort > /DIFFDIR/TEST.DIFF.txt that PHV suggested, however now it puts it like this.

231| 1|2|2| 645612| 38| 1000882| 3108| 119
231| 1|2|4| 936| 0| 1340| 593| 281
231| 1|3|1| 1331513| 8| 2386082| 2159| 137
231| 1|2|2| 647935| 38| 1003368| 3108| 119
231| 1|2|4| 944| 0| 1354| 601| 284
231| 1|3|1| 1347807| 8| 2416500| 2184| 137

I think your right, the sort would do it. I was reading the man sort. Do you think the sort -k would work to sort the first 4 columns ?

 
Unless I've misunderstood this thread, I think this might get the desired results without using diff at all...
Code:
sort TEST.yesterday.txt TEST.today.txt | uniq
This would produce the desired output in the original post.

You might also look at the "[tt]comm[/tt]" command. It can tell you what's in one file and not the other, or what's in both. It's a variation of diff.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top