How to get the number of bytes in just one line of a file?

sed -n 10p myfile | wc -c

will count the bytes in the tenth line of myfile (including the linefeed/newline character).

A slightly less readable variant,

sed -n "10{p;q;}" myfile | wc -c

(or sed '10!d;q' or sed '10q;d') will stop reading the file after the tenth line, which would be interesting on longer files (or streams). (Thanks to Tim Kennedy and Peter Cordes for the discussion leading to this.)

There are performance comparisons of different ways of extracting lines of text in cat line X to line Y on a huge file.


Try this:

line=10
tail -n "+$line" myfile | head -n 1 | wc -c

set line to the line number you need to count.


A little more straightforward using awk:

awk 'NR==10{print length($0)}' myfile