Skip to content

Column command in Linux (formatted in column, CSV or JSON)

The column command on Linux can actually save a developer a lot of time. It allows you to format a file or the output of a command in columns, CSV (separated by a comma) or JSON.

This actually saves a lot of time on data transformation.

Take, for example, the /etc/passwd file containing the system’s users, with the fields separated by colons:

$ cat /etc/passwd <br></br>root:x: 0:0:root: /root: /bin/bash <br></br>daemon:x: 1:1:daemon: /usr/sbin: /bin/false <br></br>bin:x: 2:2:bin: /bin: /bin/false <br></br>sys:x: 3:3:sys: /dev: /bin/false <br></br>sync:x: 4:65534:sync: /bin: /bin/bin/sync <br></br>:x: 5:60:games: /usr/games: /bin/false <br></br>man:x: 6:12:man: /var/cache/man: /bin/ False

The column command can be used to read the contents of this file and separate it into columns:

$ column /etc/passwd -s: -t <br></br>root x 0 0 root /root /bin/bash <br></br>daemon x 1 1 daemon /usr/sbin /bin/false <br></br>bin x ; 2 2 bin /bin /bin/false <br></br>sys x 3 3 sys /dev /bin/false <br></br>sync x 4 655 sync /bin ; /bin/sync

games x 5 60 games /usr/games /bin/false
man x 6 12 man /var/cache/man /bin/false The column command can also be used to view a CVS comma separated file:

$ cat contatos.csv | column -t -s, PHONE NAME EMAIL Uira Ribeiro (31) 3069-8315 [email protected]

This can be useful to view this type of file without the need for an Excel or similar one.

In some distributions, the column command makes it possible to export the data in JSON format, using the “-J” option, followed by the column names:

$ cat /etc/passwd | column -t -s: -J --table-columns LOGIN, PASSWORD, UID, GUID, NAME, HOME, SHELL <br></br>{<br></br>“table”: [<br></br>{"login”: “root”, “password”: “x”, “uid”: “0", “guid”: “0", “name”: “root”, “home”: “/root”, “shell”: “/bin/bash"}, <br></br>& nbsp; {"login”: “bin”, “password”: “x”, “uid”: “1", “guid”: “1", “name”: “bin”, “home”: “/bin”, “shell”: “/sbin/nologin"}, <br></br>{"login”: “daemon”, “password”: “x”, “uid”: “2", id”: “2", “name”: “daemon”, “home”: “/sbin”, “shell”: “/sbin/nologin"}, <br></br>{"login”: “adm”, “password” : “x”, “uid”: “3", “guid”: “4", “name”: “adm”, “home”: “/var/adm”, “shell”: “/sbin/nologin"}, <br></br>{"login”: “lp”, “password”: “x”, “uid”: “4", “guid”: “7", “name”: “lp”, “home”: “/var/spool/lpd”, “shell”: “/sbin/nologin"}, <br></br>{"login”: “sync”, “password”: “x”, “uid”: “5", “guid”: “0", “name”: “sync”, “home”: “/sbin”, “shell”: “/bin/sync"}, <br></br>{"login”: “shutdown”, “password”: “x”, “uid”: “6”, “guid”: “0”, “name”: “shutdown”, “home”: “/sbin”, “shell”: “/sbin”, “shell”: “/sbin” bin/shutdown <br></br>"}, {" login”: “halt”, “password”: “x”, “uid”: “7", “guid”: “0", “name”: “halt”, “home”: “/sbin”, “shell”: “/sbin/halt"},

{“login”: “mail”, “password”: “x”, “uid”: “8”, “guid”: “12”, “name”: “mail”, “home”: “/var/spool/mail”, “shell”: “/sbin/nologin”},
]
} This command can be used in conjunction with any system command to generate JSON files.

See for example how easy it is to generate a list of files from a directory in JSON:

$ ls -1 | column -t -J --table-columns FILE --table-name FILES <br></br>{<br></br>“FILES”: [<br></br>{"file”: "agradecimento.html “}, <br></br>{" file”: “thanks"}, <br></br>{"file”: "amazon.php “}, <br></br>& nbsp; {"file”: "backup.sh “},

{” file”: “certbot-auto”},
{“file”: “certificate”},
]
} In this example, the output of the ls command, with the -1 option that lists only the files one per line, was transformed by the column command into a JSON file with a column called FILE, in the FILES table.

Learn much more about Linux in our online course. You can register here. If you already have an account, or want to create one, just log in or create your user here.

Did you like it?

Share