Take the 2-minute tour ×

I got a very big file that contains n lines of text (with n being <1000) at the beginning, an empty line and then lots of untyped binary data.

I would like to extract the first n lines of text, and then somehow extract the exact offset of the binary data.

Extracting the first lines is simple, but how can I get the offset? bash is not encoding aware, so just counting up the number of characters is senseless.

share|improve this question
What do you mean by offset? Do you have to extract a range of lines? Say for example from line 10 to line 20? –  Alberto Zaccagni Apr 22 '11 at 10:33
the offset in bytes. That is, the binary data starts at byte x. –  schlange Apr 22 '11 at 10:38

4 Answers 4

up vote 5 down vote accepted

grep has an option -b to output the byte offset.


$ hexdump -C foo 
00000000  66 6f 6f 0a 0a 62 61 72  0a                       |foo..bar.|
$ grep -b "^$" foo 
$ hexdump -s 5 -C foo
00000005  62 61 72 0a                                       |bar.|

In the last step I used 5 instead of 4 to skip the newline.

Also works with umlauts (äöü) in the file.

share|improve this answer

Use grep to find the empty line

grep -n "^$" your_file | tr -d ':'

Optionally use tail -n 1 if you want the last empty line (that is, if the top part of the file can contain empty lines before the binary stuff starts).

Use head to get the top part of the file.

head -n $num
share|improve this answer

you might want to use tools like hexdump or od to retrieve binary offsets instead of bash. Here's a reference.

share|improve this answer

Perl can tell you where you are in a file:

pos=$( perl -le '
    open $fh, "<", $ARGV[0]; 
    $/ = "";  # read the file in "paragraphs" 
    $first_paragraph = <$fh>; 
    print tell($fh)
' filename )

Parenthetically, I was attempting to one-liner this

pos=$( perl -00 -lne 'if ($. == 2) {print tell(___what?___); exit}' filename

What is the "current filehandle" variable? I couldn't find it in the docs.

share|improve this answer
Don’t tell anything. Just call tell() by itself there. If you do not pass an argument to tell, it tells you the seek pointer for the last file read from using the readline operator, <>. Here it is implicit. –  tchrist Apr 22 '11 at 21:06

Your Answer


