How to print the whole line that contains a specified byte offset in a file?
With GNU awk, keep the number of bytes read so far in a variable, and when it reaches your byte offset print the current line and exit. E.g.:
$ awk -b '{ nb += length + 1 } nb >= 80 { print; exit }' file
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
The keyword length
is a shorthand for length($0)
, which returns the length of the current line in bytes (thanks to -b
). We need to add 1 to it as awk strips off the line terminator.