Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash: cat the first lines of a file & get position

Tags:

file

bash

cat

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.

like image 237
schlange Avatar asked Dec 28 '22 21:12

schlange


1 Answers

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

Example:

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

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

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

like image 134
Lesmana Avatar answered Jan 09 '23 21:01

Lesmana