Having byte offset for a file.
Is there a tool that gives line number for this byte?
- Byte count starting with zero, as in: first byte is 0 not 1.
- Line number starting with 1.
- File can have both plain text, "binary" blobs, multibyte characters etc. But the section I am interested in: End of file, has only ASCII.
Example, file:
001
002
003 <<-- first zero on this line is byte 8
004
Having byte offset 8 that would give me line 3.
Guess I could use something like this to find line number:
a. tail -c+(offset + 1) file | wc -l, here +1 as tail counts from 1.
b. wc -l file
c. Then tail -n+num where num is a - b + 1
But ... is there a, fairly common, tool that can give me num directly?
Edit, err: or the more obvious:
head -c+offset file | wc -l
0x0abytes.:echo byte2line(offset+1).vim -bandvim+set binary+ open file it got corrupted. (Ah. Suddenly I recall which plugin messes it up). But, anyway, as I use this in batches and in combination with a range of scripts Vim was early abandoned. But +1 anyways.0xaanywhere. The concept of lines in a binary file is meaningless.