Tabs to Spaces and Back, Measured: expand vs unexpand vs sed vs awk vs perl
The fastest tool is also the only one that aligns to tab stops; the naive substitution matches it exactly until a tab sits off a tab stop; and the flag that sets the tab width silently switches off leading-only conversion.
Written November 10, 2026 · 15 min read
- The table: expand is fastest, and the naive arms agree byte for byte
- The closest published work explains in prose, with no table
- The tab that sits off a tab stop
- The unexpand that only converts leading blanks
What this guide is
Two corpora, ten arms, three reps each. The main corpus is 1,000,000 lines, 24,700,562 bytes, with 1,926,492 tabs: 1,626,906 leading tabs at depths of 0 to 4, and 299,586 mid-line tabs, one on about 30 percent of lines. Every tab in it sits at a multiple-of-8 column, because each mid-line tab follows text padded to the next multiple of 8. The second corpus is the same size and the same random stream, 23,507,886 bytes, but its mid-line tabs are unpadded: the tab follows the words directly and sits at an arbitrary column. Every arm converts the file, and every rep is checked the same way: line count, tab count, byte count, and a sha256 of the whole output, so a timing number can never come from a run that printed something different. The question is the one people ask before converting indentation in a file: which tool turns tabs into spaces fastest, which turns spaces back into tabs, and which of the obvious one-liners change something else while looking complete.
The answer is a correction in three directions. expand is the fastest tabs-to-spaces arm at 0.11 seconds, and the only one that aligns to tab stops. A naive s/\t/ /g in sed, awk, or perl is byte-identical to it whenever every tab sits at a multiple of 8, and silently misaligns 266,335 of 1,000,000 lines the moment a tab sits anywhere else. Bare unexpand converts leading blanks only and leaves all 299,586 mid-line lines untouched, but passing -t 8, the same nominal width, silently switches off leading-only and converts mid-line blanks exactly like -a. And the expand-then-unexpand round-trip is column-equivalent but not byte-identical: it comes back 926,341 bytes shorter with 266,335 extra tabs, so a byte comparison after a round-trip fails on a file that is visually identical.
The table: expand is fastest, and the naive arms agree byte for byte
Wall seconds, medians of three runs, fresh tonight. Every rep returned rc=0, and every arm's output sha256 was identical across its three reps.
Job A, tabs to spaces, main corpus, 24,700,562 bytes in, 38,186,006 bytes out except where noted:
expand: fresh median 0.1088, reps 0.1065, 0.1088, 2.9184. Banking run: 0.1185. Fastest rep 0.1065.expand -t 4: fresh median 0.0977, reps 0.0977, 0.0977, 4.9109. Banking run: 0.0975. Output 30,480,038 bytes, the narrower width.sed 's/\t/ /g': fresh median 0.5809, reps 0.3009, 0.5809, 4.9400. Banking run: 0.3039. Fastest rep 0.3009.perl -pe 's/\t/ /g': fresh median 0.3858, reps 0.3858, 0.3786, 6.3046. Banking run: 0.3870. Fastest rep 0.3786.awk '{gsub(/\t/, " "); print}': fresh median 3.3796, reps 0.5421, 3.8696, 3.3796. Banking run: 0.5259. Fastest rep 0.5421.
Against expand at its fastest rep of 0.1065 seconds tonight, sed costs 2.83 times, perl 3.55 times, and awk 5.09 times. The banking run's medians give 2.57, 3.27, and 4.44. The two runs agree on the ranking and on the size of the gaps within about half a multiple: sed about 2.6 to 2.8 times, perl about 3.3 to 3.6, awk about 4.4 to 5.1. The second corpus corroborates the same ranking at its fastest reps: expand 0.1066, sed 0.2919, perl 0.3675, awk 0.5251. expand -t 4 ties expand at 0.82 times its cost on the banking medians, so the width changes the output size, not the speed. Throughput on the 24.7 MB input at the fastest reps: expand reads about 232 MB/s, sed about 82 MB/s, perl about 65 MB/s, awk about 46 MB/s.
Job B, spaces to tabs, input the expanded file, 38,186,006 bytes, 0 tabs:
unexpand: fresh median 0.1576, reps 0.1222, 0.1652, 0.1576. Banking run: 0.1286. Output 26,797,664 bytes, 1,626,906 tabs.unexpand -a: fresh median 0.2126, reps 0.2126, 0.2121, 3.0542. Banking run: 0.2126, identical. Output 23,774,221 bytes, 2,192,827 tabs.unexpand -t 8: fresh median 0.2194, reps 0.2117, 0.2194, 2.3731. Banking run fastest rep: 0.2078. Output 23,774,221 bytes, the same as-a.unexpand -t 4: fresh median 0.3644, reps 0.2170, 0.3644, 2.9624. Banking run fastest rep: 0.2254. Output 25,845,861 bytes.unexpand -a -t 4: fresh median 0.2138, reps 0.2088, 0.2138, 2.4591. Banking run: 0.2187. Output 25,845,861 bytes, the same as-t 4.
Against unexpand at its fastest rep of 0.1222 seconds tonight, every arm that converts mid-line blanks costs about 1.7 times: -a 1.74, -t 8 1.73, -t 4 1.78, -a -t 4 1.71. The banking run gives 1.65, 1.62, about 1.7, and 1.70. The gap is the same on both runs: converting mid-line blanks costs roughly two-thirds more than converting leading blanks only.
Two cells are noisy and the guide says so rather than hiding it. The box was under intermittent load tonight, as on the banking run: third-rep spikes of 2.37 to 6.30 seconds hit 8 of the 14 arms, sed rep 2 is 0.5809 against a fastest rep of 0.3009, and awk reps 2 and 3 are 3.87 and 3.38 against a fastest rep of 0.5421. The fresh medians for sed, awk, and unexpand -t 4 are load-contaminated and are not usable as canonical figures; the guide compares on fastest reps, which match the banking medians within a few percent on every clean arm. The 0.10 to 0.53 second Job A cells sit near the spawn-and-read floor, so treat the exact ratios as order-of-magnitude; the ranking and the byte-identical outputs are the stable claims.
The closest published work explains in prose, with no table
The closest published work is a methods guide, not a measurement, and the distinction is the gap this probe fills. The NetworkWorld piece walks through expand and unexpand on small sample files and compares them with sed and awk in prose, showing that expand aligns to tab stops while sed changes one tab to one space, with no timings, no corpus, no reps, and no output verification. The learnbyexample coreutils page is the most thorough prose reference: it shows expand aligning to multiples of 8 with worked examples, documents expand -i and the -t list forms, and states that unexpand -a converts all blanks while bare unexpand stops at the first non-blank character. It also states, in one line, that the -a option is implied when -t is used. It runs no timings and prints no table. The Putorius unexpand piece explains the leading-only default in prose, quotes the coreutils manual on -a, and shows -t 2 and -t 1 on a small script, again with no table and no mention of what -t does to the leading-only default beyond the width. This probe measures what those pages state: the naive substitution against expand on one corpus with reps and verified outputs, the leading-only counts, and the -t behavior with its exact byte counts.
The tab that sits off a tab stop
expand aligns each tab to the next multiple of 8, and the manual says so directly: with no -t, tabs are 8 characters apart. A naive substitution replaces every tab with exactly 8 spaces, which is the same thing only when the tab already sits at a multiple of 8. On the main corpus, where every tab sits at a multiple of 8, the three naive arms are byte-identical to expand: the same sha256, 0 differing lines, all three arms. On the second corpus, where the mid-line tabs sit at arbitrary columns, expand and each naive arm differ on 266,335 lines, all three arms, the same count, the same output hash. The first differing line is line 1: expand gives two leading tabs as 16 spaces, then lima cache xray uniform, then one space, then oscar uniform, because the tab sat at column 39 and expand adds 1 space to reach tab stop 40. The naive arms give the same words with 8 spaces before oscar. The naive output is 938,004 bytes larger on the second corpus, 36,993,330 against 36,055,326, and every misaligned column is off by up to 7 spaces.
The practical rule follows from the counts, not from the speed table. If every tab in the file sits at a multiple of 8, which is true of files indented by tabs alone, the naive substitution is correct and expand wins only on speed, at about a third of the sed cost. If any tab sits anywhere else, as in a data file with tab-separated columns after variable-length words, the naive substitution silently shifts every column after it, and only expand keeps the columns aligned. The trap is invisible on tab-stop-aligned input and fires on 27 percent of lines otherwise, so a test on indented code proves nothing about a test on tab-separated data.
The unexpand that only converts leading blanks
Bare unexpand converts initial blanks only, and the manual says so directly: -a converts all blanks instead of just initial blanks. On the expanded file, bare unexpand writes 1,626,906 tabs and leaves all 299,586 mid-line lines untouched: the 9-space mid-line runs in the expanded file stay 9 spaces. unexpand -a writes 2,192,827 tabs, and the two outputs differ on 299,586 lines, first line 1. The speed cost of converting those mid-line blanks is about 1.7 times the leading-only arm, 0.2126 against 0.1286 on the banking medians. The practical rule is the mirror of the expand one: if the file is indented with spaces and nothing else needs converting, bare unexpand is the cheapest correct tool; if the spaces to convert sit after text on the line, bare unexpand silently does nothing to them, and -a is the tool, at about 1.7 times the cost.
The -t that silently switches off first-only
Passing an explicit tab width to unexpand changes more than the width. unexpand -t 8 output is byte-identical to unexpand -a output: the same sha256, 0 differing lines, 299,586 lines differing from bare unexpand. The same holds at width 4: -t 4 against -a -t 4 is 0 differing lines with the same output hash. The man page states it, in three words in parentheses after the -t description: -t enables -a. The learnbyexample page states it in prose: the -a option is implied when -t is used. Neither states it loudly, and the Putorius piece never mentions it, so a reader who passes -t 8 to set the width while expecting leading-only conversion gets mid-line conversion with no warning. The small case shows it at one line: foo plus 5 spaces plus X stays unchanged under bare unexpand, becomes foo plus a tab plus X under -t 8, and stays unchanged again under --first-only -t 8. The --first-only flag restores leading-only conversion and overrides -a, so the fix for a width change that must stay leading-only is to pass both flags. The finding is GNU coreutils 9.4 behavior, verified; BSD unexpand may differ, so do not generalize it.
The round-trip that is column-equivalent, not byte-identical
unexpand -a of the expanded file does not reproduce the main corpus: 23,774,221 bytes against 24,700,562, a difference of 926,341 bytes, and 2,192,827 tabs against 1,926,492, a difference of 266,335, exactly the mid-line-tab count. The padding spaces that preceded each mid-line tab collapse into extra tabs: a 4-space pad plus the tab's 8 spaces become 2 tabs where the original had spaces plus 1 tab. But expand of that round-tripped file is byte-identical to the expanded input, so the round-trip preserves every column. A byte comparison after a round-trip fails on a file that is visually identical, and the failure is the padding design, not data loss: the columns are the same, the bytes are not. The practical rule is to compare round-trips by re-expanding and comparing, or by comparing columns, never by comparing the round-tripped bytes to the original.
Checklist
- Before replacing
expandwith asedsubstitution, check where the tabs sit: the substitution is byte-identical toexpandwhen every tab sits at a multiple of 8, and misaligns 266,335 of 1,000,000 lines otherwise, off by up to 7 spaces per column. - Before running bare
unexpandon a file, check whether the blanks to convert sit after text: bareunexpandleaves all 299,586 mid-line lines untouched, and-ais the tool that converts them, at about 1.7 times the cost. - Before passing
-ttounexpand, check the leading-only default:-t 8behaves exactly like-aon this corpus, 0 differing lines, and only--first-only -t 8restores leading-only conversion. - Before comparing a round-trip byte for byte, check the columns: the round-trip comes back 926,341 bytes shorter with 266,335 extra tabs, and
expandof the round-tripped file is byte-identical to the expanded input. - Before quoting a ratio from this guide, check both runs: the box was under load on both, the fresh
sedandawkmedians are load-contaminated, and the ranking with the fastest reps is the stable claim. - Before reusing these numbers, check the setup: WSL2, 32 CPUs, coreutils 9.4, sed 4.9, awk 5.2.1, Perl 5.38.2, tmpfs, warm page cache, one thread per arm, two 1,000,000-line tab corpora with 30 percent mid-line tabs, hashes recorded in the probe log.
Related guides on this site
- Editing a File In Place, Measured: sed -i vs perl -pi vs awk
- Converting CRLF to LF, Measured: tr vs sed vs perl vs awk
- Finding Lines by Prefix: look vs grep vs rg vs awk vs sed, Measured
- Cutting a Million-Line File Into Ten, Measured: split vs csplit vs awk
Sources
- Linux man-pages, expand(1) — read on the box 2026-11-10: converts tabs to spaces,
-tsets tabs N characters apart instead of 8,-iconverts initial tabs only. Box runs GNU coreutils 9.4; every cited behavior was exercised by the probe itself. - Linux man-pages, unexpand(1) — read on the box 2026-11-10:
-aconverts all blanks instead of just initial blanks,--first-onlyconverts only leading blanks and overrides-a,-tsets tabs N characters apart instead of 8 and enables-a. Box runs GNU coreutils 9.4; every cited behavior was exercised by the probe itself. - learnbyexample, expand and unexpand — CLI text processing with GNU Coreutils — read 2026-11-10: worked examples of
expandaligning to multiples of 8,expand -i, the-tlist forms,unexpandstopping at the first non-blank character, and-aconverting all blanks; states that-ais implied when-tis used. No timings, no corpus, no reps, no table; the closest prose reference, and the gap this probe fills. - Putorius, Unexpand — Convert Spaces to Tabs on Linux Command Line — read 2026-11-10: explains the leading-only default in prose, quotes the coreutils manual on
-a, shows-t 2and-t 1on a small script. No table, no timings, no mention of-tswitching off leading-only. - NetworkWorld, Turn tabs into spaces on Linux and vice versa — read 2026-11-10: walks through
expandandunexpandon small sample files and compares them withsedandawkin prose, noting thatexpandaligns to tab stops whilesedchanges one tab to one space. No timings, no corpus, no reps, no table. - Perldoc, perlrun — read 2026-11-10:
-passumes a loop around the program that prints each line,-esupplies the program on the command line. Box runs Perl 5.38.2; the-peform in this guide was exercised by the probe itself. - Veldica, Converting CRLF to LF: tr vs sed vs perl vs awk, Measured — and the Carriage Return That Isn't at the End of a Line — the sibling guide: measures the same
sed,perl, andawkarms on a different transformation, line-ending conversion, and found the same byte-identical trio pattern. This guide converts tabs; it does not convert line endings. - Veldica, Seeing the Hidden Characters: cat -A vs sed -n l vs od -c, Measured — and the Long Line That Folds Into Three — the sibling guide: measures the same tabs as characters to display, with
cat -Ashowing every tab in its corpus. This guide converts the tabs; it does not display them. - Veldica, Numbering a Million Lines: cat -n vs nl vs awk vs grep -n, Measured — and the Line That Eats All Your Numbers — the sibling guide: measures the same corpus-scale harness on a different operation, line numbering, and found the same byte-identical-pair pattern with
nl -bamatchingcat -n. This guide converts whitespace; it does not number lines.
Keep Exploring
Paste a [Service] section and get a plain-language verdict on every sandboxing setting it finds.
Paste sshd -G output and get a hardening verdict on the values your server actually runs.
Paste a server block and get WebSocket, TLS, HSTS, and proxy-header verdicts.
Paste a systemd timer expression and get the specific reason it fails, the normalized form, and the past-date traps the built-in check hides.
The full sysadmin hub \u2014 systemd, cgroups, file tools, and measured Linux comparisons.