3

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

Gautam
  • 2,295
  • 5
  • 18
  • 18
  • 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](http://unix.stackexchange.com/questions/11343/linux-tools-to-treat-files-as-sets-and-perform-set-operations-on-them). –  Jan 14 '12 at 12:41

2 Answers2

3

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.

Mat
  • 51,578
  • 10
  • 158
  • 140
  • 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
1

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.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175