6

How would I go about truncating a binary file when a certain pattern is found?

For instance, I want to truncate the file at the first occurrence of the pattern 0xFFFFFFFF.

I think something like awk could do the trick... but I'm not exactly sure how.

thanks

2 Answers 2

5

Convert to octal and assign to the record separator.

awk 'BEGIN{RS="\377\377\377\377"} NR==1 {print;exit}'
0
1

I'm quite sure, it's possible using Perl when you do the following

  • use the command line option -0777 to slurp all the input at once
  • use Latin-1 coding
  • use s/\xFF\xFF\xFF\xFF.*//s

I did something like this a long time ago, but I don't recall the options anymore. For awk I have no idea.

2
  • Oh, awk is not a requirement, any solution will do. I'm not very good at Perl, could you provide a simple example?
    – nico
    Commented Mar 1, 2011 at 12:52
  • 1
    Something like perl -0777 -pe 's/\xFF\xFF\xFF\xFF.*//s' < infile > outfile, but I don't know how to set the encoding now.
    – maaartinus
    Commented Mar 1, 2011 at 13:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .