Intro To 'expand' Command In Linux
2023-06-09 - By Robert Elder
I use the 'expand' command to replace tabs with spaces:
echo -n $'\t' | expand | xxd
00000000: 2020 2020 2020 2020
Expand Tabs Into Spaces
Here, I have an 'echo' statement that prints out a single tab character between the letters 'a' and 'b':
echo -en "a\tb"
echo -en "a\tb" | xxd
00000000: 6109 62 a.b
If I pipe this 'echo' statement into the 'expand' command, the single tab character will be replaced by eight space characters:
echo -en "a\tb" | expand | xxd
00000000: 6120 2020 2020 2020 62 a b
Default Use Case & Custom Tab Width
Here, I have a file called 'numbers.txt' that contains several columns of random numbers:
29303 32454 7898 4022
11773 16085 19539 32199
16335 23913 17824 14969
25557 15589 2375 8899
With the 'expand' command, the default number of spaces to replace each tab with is 8:
cat numbers.txt | expand
29303 32454 7898 4022
11773 16085 19539 32199
16335 23913 17824 14969
25557 15589 2375 8899
I can specify a different number like 12 with the '-t' flag:
cat numbers.txt | expand -t 12
29303 32454 7898 4022
11773 16085 19539 32199
16335 23913 17824 14969
25557 15589 2375 8899
Initial Tabs
The 'expand' command can treat 'initial' tabs differently when they start at the beginning of a line before any non-blank characters. Let's add one extra line to the file above:
echo -e "1234\t5678" >> numbers.txt
so now it looks like this:
29303 32454 7898 4022
11773 16085 19539 32199
16335 23913 17824 14969
25557 15589 2375 8899
1234 5678
Now, I can include the '-i' flag to cause only the initial tabs to be replaced:
cat numbers.txt | expand -i -t 12
29303 32454 7898 4022
11773 16085 19539 32199
16335 23913 17824 14969
25557 15589 2375 8899
1234 5678
And here is the hexadecimal output from above:
cat numbers.txt | expand -i -t 12 | xxd
00000000: 2020 2020 2020 2020 2020 2020 3239 3330 2930
00000010: 3309 3332 3435 3409 3738 3938 0934 3032 3.32454.7898.402
00000020: 320a 2020 2020 2020 2020 2020 2020 3131 2. 11
00000030: 3737 3309 3136 3038 3509 3139 3533 3909 773.16085.19539.
00000040: 3332 3139 390a 2020 2020 2020 2020 2020 32199.
00000050: 2020 3136 3333 3509 3233 3931 3309 3137 16335.23913.17
00000060: 3832 3409 3134 3936 390a 2020 2020 2020 824.14969.
00000070: 2020 2020 2020 3235 3535 3709 3135 3538 25557.1558
00000080: 3909 3233 3735 0938 3839 390a 3132 3334 9.2375.8899.1234
00000090: 0935 3637 380a .5678.
A Practical Example
Here, I have a tab separated document called 'living-expenses.txt':
Category Jan Feb Mar
Food $102.78 $97.34 $112.42
Rent $380.00 $380.00 $380.00
Linux Weekly News Subscription $9.00 $9.00 $9.00
Artistic Anime Figurines $1,749.38 $2,109.87 $3,829.48
This document is hard to read because some of the entries are much longer than eight characters.
I can provide a list of column offsets to the '-t' flag so the tab characters will be expanded with as many spaces as necessary to reach the specified offset for each column.
cat living-expenses.txt | expand -t 1,33,43,53,63
Category Jan Feb Mar
Food $102.78 $97.34 $112.42
Rent $380.00 $380.00 $380.00
Linux Weekly News Subscription $9.00 $9.00 $9.00
Artistic Anime Figurines $1,749.38 $2,109.87 $3,829.48
And that's why the 'expand' command is my favourite Linux command.
Intro To 'stty' Command In Linux
Published 2023-10-04 |
$1.00 CAD |
Intro To 'nproc' Command In Linux
Published 2023-07-15 |
Intro To 'comm' Command In Linux
Published 2023-09-06 |
How To Force The 'true' Command To Return 'false'
Published 2023-07-09 |
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
Intro To 'chroot' Command In Linux
Published 2023-06-23 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|