Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have 2 Binary Files FileA and FileC

Its is such that FileC = FileA + FileB using the cat utility

How do I subtract FileA from FileC to get FileB ?

PS: I am using Ubuntu Oneiric

share|improve this question
    
they are binary files, so it can get hard. If it is a text file, it is possible to use little bit of grep or perl stuff to get back fileB. –  Nikhil Mulley Jan 14 '12 at 12:34
    
As @Nikhil said, it would be possible with a regular file to treat them as sets and do set operations on them. –  Herman Torjussen Jan 14 '12 at 12:41
add comment

2 Answers

up vote 3 down vote accepted

Assuling you have stat on your plateform to get the size of FileA, you could do something like:

dd if=./FileC of=./FileB bs=1 skip=$(stat -c %s ./FileA)

which should work on any type of file.

share|improve this answer
    
This works assuming that content of FileA was put or concatenated into FileC before FileB. But works for me, because its me who will put FileA before FileB into FileC. +1 :-) –  Nikhil Mulley Jan 14 '12 at 13:15
add comment

You need to know where to cut. For binary files, this generally means knowing the size of FileA or FileB.

You can find the size of FileA with ls -l. If you need to write a portable script, you can extract the size with ls -lgo FileA | awk '{print $3; exit}' (or, for non-POSIX-compliant versions of ls that don't have the -g and -o options, ls -l FileA | awk '{print $5; exit}'). On non-embedded Linux, a simpler way to obtain the size is stat -c %s FileA.

Once you have the size, you can use tail to extract the second part of the file:

tail -c +$((sizeA + 1)) <FileC

If you want to break a file into equal chunks, use the split command.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.