The Column command in Linux is a very useful tool that supports formatting output to make column data more readable. You can split consecutive lines of text in an input stream into multiple columns of output, similar to TAB or space-separated values.
Basic usage of the Column command:
column < inputfile
This command formats the contents of the input file into multiple columns of output.
Autotab:
column -t < inputfile
Column-t can automatically detect tabs in the input and format columns separated by tabs.
The -s option allows you to specify a custom delimiter, using a colon as an example:
column -s ':' < inputfile
The column width can be specified with -c, which allows the maximum width of the column to be specified, such as 10 characters:
column -l < inputfile
Left-justified, the -1 option makes the output columns left-justified instead of the default right-justified:
column -l < inputfile
Use Column-N to output without padding characters:
column -N < inputfile
The seq command can be used to generate numbers from 1 to 20, which are piped to the Column command, -t separates the output TAB, and -c 4 specifies a group of four columns.
The following command lists all files in the current directory and outputs them in tab-separated column format. -t makes the output tabular:
ls -l | column -t
Using commas as custom delimiters, output in tabular form:
column -s ',' -t < inputfile
You can merge multiple tables. After combining the contents of multiple files, use the column command to output the contents in tab-separated column format:
cat file1 file2 | column -t
Can be combined with other commands to display all currently running processes, and output in the tab-separated column format:
ps aux | column -t
The Column command works with files that contain multiple separators. Column can identify only one delimiter. To process multiple delimiter files, use tools such as awk or sed to preprocess the files, unify all delimiters into one, and then use the Column command. Start with the awk command:
awk '{gsub(/[,;:|]/, FS); print}' inputfile | column -t -s,,
Here the gsub function is used to take all occurrences of delimiters (in this case, commas, semicolons; , colons: and the vertical bar |) are replaced with the field separator FS (the default is space), and the column command is then output as a table with the -t option and specifies a comma as the separator with -s,.
Re-use sed:
sed 's/[,;:|]/ /g' inputfile | column -t
Here the sed command replaces all delimiters with Spaces, and the column command is formatted with the default space delimiter.
Replace the delimiter with tr. tr can be used to delete or replace characters, replacing all delimiters with a unified character:
tr -s '[:punct:]' ' ' < inputfile | column -t
The above command can replace all punctuation marks with Spaces, and then use the Column command to format them with space delimiters.
If different columns in the file use different separators, paste and awk can be used to handle this:
paste -d, - - - < inputfile | awk -F, '{print $1, $2, $3}' | column -t
paste is used as the separator to merge the file columns, then awk is used to process the merged rows, and finally the column command is used to format the output.
When working with multiple delimiter files, determine which delimiter is most common or which is best suited as a unified delimiter. The preprocessing steps may vary according to the content and format of the file. Therefore, adjust the regular expressions of commands such as awk, sed, and tr according to the actual situation. Preprocessing may increase processing time, especially for large files that require further trade-offs between processing efficiency and result accuracy.