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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare 2 text files

Status
Not open for further replies.

turnersn

Programmer
Apr 11, 2000
6
0
0
US
Visit site
2 text files where:
file1 has a record length of 23
file2 has a record length of 33

I want to compare the 2 files so that records in file1 exist in file2. Essentially I want to compare the files utilizing only the first 23 bytes of the records in each file. Is this possible via a UNIX script?
 
If I understand you correctly:

dd if=file1 of=/tmp/file1 bs=1 count=23
dd if=file2 of=/tmp/file2 bs=1 count=23
diff /tmp/file1 /tmp/file2

? Tony Lawrence
SCO Unix/Linux Resources tony@pcunix.com
 
I do not think I explained this well enough. I am trying to take the 10 bytes from file2 and put it on the tail of file1 records where the first 23 bytes of file1 = first 23 bytes of file2.

What I somewhat envisioned doing was a file compare between file1 and file2, but only do the compare on the first 23 bytes. That way, directing my output to another file I could use figure out the position of the '|' char and then write a awk script saying where position 'x' = '|' write ($0, position, length)
 
Huh?

I'm more confused now than I was before :)

The script I gave you compares the first 23 bytes. Now you say you want to tack on 10 bytes from file2 if that comparison matches. OK, that's just

dd if=file1 of=/tmp/file1 bs=1 count=23
dd if=file2 of=/tmp/file2 bs=1 count=23
diff -q /tmp/file1 /tmp/file2 || dd if=file2 bs=1 count=10 >> file1


If not, give an example of the two files and an example of what you want to end up with.


Tony Lawrence
SCO Unix/Linux Resources tony@pcunix.com
 
An example is best I think:

File1 File2
12345678901234567890123 123456789012345678901234567890123
01234567890123456789012 212345678901234567890123456789012
23456789012345678901234 234567890123456789012345678901234

For the above, I want to create a file with only record1 and record3 from file2 because the first 23 bytes of file1 and file2 match. I do not want record2 of file2 in this file because the first 23 bytes of file1 and file2 do not match.
-q is not a legal option for diff. I am using Sun Solaris 5.6 I think.
 
Then look at the man page and find out what option just reports status- it might be -s for example. Tony Lawrence
SCO Unix/Linux Resources tony@pcunix.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top