6

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?

3 Answers3

14
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.

Stephen Kitt
  • 434,908
  • 1
    nice. i was going to say sed '10q;d' myfile | wc -c', but i like the readability of sed -n 10p. – Tim Kennedy Nov 22 '16 at 17:59
  • 4
    @TimKennedy quitting straight after the requested line as in your variant would be better on large files! I think there's a question here with benchmarks. – Stephen Kitt Nov 22 '16 at 18:10
  • I seriously need to study SED a lot more . . . . Thanks guys!!!1 – chromechris Nov 22 '16 at 18:56
  • 2
    Good point about quitting right away. sed doesn't try to figure out whether the given sed commands will ever print any more output. yes | sed -n '10{p;q} | wc -c has a nice combo of readability and performance. It exits right away after seeing line 10. yes | sed -n 10p | wc -c runs forever. – Peter Cordes Nov 23 '16 at 03:42
6

Try this:

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

set line to the line number you need to count.

jayhendren
  • 8,384
  • 2
  • 33
  • 58
5

A little more straightforward using awk:

awk 'NR==10{print length($0)}' myfile
Kevin
  • 40,767
  • Suggestion: exit after printing, instead of reading the rest of the file. yes | awk 'NR==10{print length($0); exit}' exits right away, but yours loops forever (given infinite input). Or with a file, wastes time reading the whole rest of the file from disk. – Peter Cordes Nov 23 '16 at 03:45
  • 1
    @PeterCordes better yet (IMO), skip the rest of the file with nextfile — that way the AWK script can be used to process multiple files! – Stephen Kitt Nov 23 '16 at 13:00
  • @StephenKitt: Yes, that would be a better design. Good thinking. – Peter Cordes Nov 23 '16 at 13:06
  • Or just awk 'NR == 10 {print length; exit}'. That's different from the sed|wc solution in that it counts the number of characters as opposed to the number of bytes and doesn't count the line delimiter. – Stéphane Chazelas Nov 23 '16 at 13:19