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?

4
  • 3
    How are you identifying the line you want to count the bytes in? – pgoetz Nov 22 '16 at 17:46
  • 1
    Do you really mean bytes or did you intend to ask for characters? echo -n '€' | wc -c -m will return 3 bytes, 1 character. – roaima Nov 22 '16 at 18:00
  • Yea, actual bytes. – chromechris Nov 22 '16 at 18:58
  • The answers given assume you know the line number of the target line ahead of time. Is this correct, or are might you need to select a target line based on some other criteria? – Kevin Fegan Nov 22 '16 at 22:06
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.

4
  • 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 3: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.

3
5

A little more straightforward using awk:

awk 'NR==10{print length($0)}' myfile
4
  • 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 3: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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.