Skip to content
shellhacks.dev
Performance

Count lines in a huge file fast

Count the lines of a big file with wc -l, which is the fastest of the usual tools. It counts newlines, so a last line without a newline is not counted.

2 min read
Summarize in AI

A file with millions of lines needs a line count, and the usual tools differ a good deal in speed.

Shell
seq 1 5000000 > big.txt
timeit() { local s=$EPOCHREALTIME e n l; n=$("$@" < big.txt); e=$EPOCHREALTIME; printf -v l '%s ' "$@"; printf '%-22s %d lines  %.2f s\n' "$l" "$n" "$(awk -v a="$s" -v b="$e" 'BEGIN{print b-a}')"; }
timeit wc -l
timeit grep -c ''
timeit awk 'END{print NR}'
timeit sed -n '$='
echo ---
printf 'a\nb' | wc -l
printf 'a\nb' | grep -c ''
Text
wc -l                  5000000 lines  0.02 s
grep -c                5000000 lines  0.08 s
awk END{print NR}      5000000 lines  0.20 s
sed -n $=              5000000 lines  0.24 s
---
1
2

wc -l counts newline characters in large blocks and does no parsing, so it is the fastest. grep -c '' splits the input into lines, awk and sed do that and run an interpreter loop, which accounts for the order in the timings above on a 38 MB file of five million lines. The -l option is in the coreutils manual. The last pair shows the difference in meaning: wc -l counts newlines, and a final line without one is not counted.

The timings were taken on an arm64 container with Bash 5.2, coreutils 9.1 and GNU grep 3.8, and one run each, so read them as an order of magnitude. The file was already in the page cache, which a first read from disk is not. For an exact count of the lines of text including an unterminated last one, use grep -c '' or awk.

Share

More snippets

Performance

Trace file opens of a command

Find which files a command opens with strace -e trace=openat, and add -e status=failed to see the config files it looked for and did not find.

2 min read
Performance

Count process spawns in a script

Count how many processes a shell script starts with strace -f -c -e trace=execve,clone. A loop that calls date five times runs six execs, and a builtin runs one.

1 min read
Performance

Find syscalls that take time

Show how long each system call takes with strace -T. The figure in angle brackets is the time spent inside the call, so a slow write or sleep stands out.

1 min read

Get new posts by email

One email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.