Bash: cat the first lines of the file and get the position

I have a very large file that contains n lines of text (with n and <1000) at the beginning, an empty line, and then a lot 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 encoded, so just counting the number of characters is pointless.

+4
source share
4 answers

grep has the -b option to output a 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 a new line.

Also works with umlauts (Àâü) in a file.

+5
source

Use grep to find an empty string

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

Use tail -n 1 if necessary if you want to use the last empty line (that is, if the top of the file may contain empty lines before running the binary file).

Use head to get the top of the file.

 head -n $num 
+3
source

you can use tools like hexdump or od to extract binary offsets instead of bash. Here is the link .

+1
source

Perl can tell you where you are in the file:

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

In turn, I tried to do a single line

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

What is a current file variable? I could not find it in the documents.

+1
source

All Articles