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

newlinestext processingwc

I am wondering how I can get the number of bytes in just one line of a file.

I know I can use wc -l to get the number of lines in a file, and wc -c to get the total number of bytes in a file. What I want, however, is to get the number of bytes in just one line of a file.

How would I be able to do this?

Best Answer

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.

Related Question