This tutorial explains Linux “expand” command, options and its usage with examples.
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]…
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”.
$ 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.
- Practice Programming MCQs
- Apply for Programming Internship
- Check Information Technology Books
- Check Linux Books