expand Command in Linux

This tutorial explains Linux “expand” command, options and its usage with examples.

expand – convert tabs to spaces.

Description :

Convert tabs in each FILE to spaces, writing to standard output. With no FILE, or when FILE is -, read standard input.

Usage :

expand [OPTION]… [FILE]…

advertisement
advertisement

Options :

-i, –initial
do not convert tabs after non blanks
-t, –tabs=NUMBER
have tabs NUMBER characters apart, not 8
-t, –tabs=LIST
use comma separated list of explicit tab positions

Examples :

1. Basic Example

$ cat file1
One	Two	Three	Four	Five	six	seven
One	Two	Three	Four	Five	six	seven
One	Two	Three	Four	Five	six	seven
One	Two	Three	Four	Five	six	seven
One	Two	Three	Four	Five	six	seven
One	Two	Three	Four	Five	six	seven
One	Two	Three	Four	Five	six	seven
One	Two	Three	Four	Five	six	seven
 
$ cat -vet file1
One^ITwo^IThree^IFour^IFive^Isix^Iseven$
One^ITwo^IThree^IFour^IFive^Isix^Iseven$
One^ITwo^IThree^IFour^IFive^Isix^Iseven$
One^ITwo^IThree^IFour^IFive^Isix^Iseven$
One^ITwo^IThree^IFour^IFive^Isix^Iseven$
One^ITwo^IThree^IFour^IFive^Isix^Iseven$
One^ITwo^IThree^IFour^IFive^Isix^Iseven$
One^ITwo^IThree^IFour^IFive^Isix^Iseven$

By using the “cat -vet” option against file1, we can see that the tabs are represented by the “I^” character.The “$” denotes a line feed. Now if we use the expand command, we can convert these tabs into spaces. We will use the expand command and create a new file called “file2”.

advertisement
$ expand file1 > file2
$ cat -vet file2
One     Two     Three   Four    Five    six     seven$
One     Two     Three   Four    Five    six     seven$
One     Two     Three   Four    Five    six     seven$
One     Two     Three   Four    Five    six     seven$
One     Two     Three   Four    Five    six     seven$
One     Two     Three   Four    Five    six     seven$
One     Two     Three   Four    Five    six     seven$
One     Two     Three   Four    Five    six     seven$

2. Converts the tabs in the file myfile.txt to 10 spaces each, and write the output to myfile2.txt

$ expand --tabs=10 myfile.txt > myfile2.txt

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

advertisement
If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.