indent.txt uses leading spaces corresponding to tab stops of eight columns.
Replace convertible leading spaces with tabs while preserving display positions.
Which GNU coreutils command is appropriate?
unexpand converts spaces to tabs, while expand performs the opposite conversion. The -t option sets tab stops.
Detailed explanation
expand -t 8 indent.txtIncorrect. expand converts tabs into spaces, the opposite direction.
tr ' ' '\t' < indent.txtIncorrect. Replacing every space one-for-one with a tab does not preserve columns.
unexpand -t 8 indent.txtCorrect. unexpand -t 8 converts suitable leading spaces using tab stops of eight.
cut -c9- indent.txtIncorrect. cut -c9- removes leading characters and loses indentation.
Try it yourself
An example you can run in a temporary verification environment.
printf ' item\n' | unexpand -t 8 | od -An -tx1Expected result
先頭バイトが09(tab)、続いてitemと改行Key points
- unexpand converts spaces to tabs
- expand converts tabs to spaces
- -t sets tab stops
Notes
- Environment: GNU coreutils 9.x / 標準入力
- Use small, disposable text streams when testing filters.
- Locale, encoding, and GNU/POSIX implementation details can affect edge cases.
Foundation review
Place the operation
unexpand is the reverse-direction tool for restoring tabs from suitable space runs.
Check the side effect
Inspect visible whitespace or use a byte-oriented display to distinguish tabs from spaces.