“expand” Command Usage Examples in Linux

«
»

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

expand – convert tabs to spaces.

Description :

advertisement

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 :

advertisement

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.

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!
advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter