fbpx column Command in Linux: Format Output in Columns and CSV | Certificação Linux

The column Command in Linux: Format Output in Columns, CSV, and JSON

📅 12/06/2021 ⏱ 4 min ✍️ Uira Ribeiro
Linuxcommands
The column Command in Linux: Format Output in Columns, CSV, and JSON
Exam Pass App — mais de 7.000 questões LPI, offline, em 5 idiomas, disponível no Android e na App Store
Available on
Google Play
Available on
App Store

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

This really saves a lot of time in data transformation.

For example, see the /etc/passwd file which contains the system users, with fields separated by colons:

$ cat /etc/passwdroot:x:0:0:root:/root:/bin/bashdaemon:x:1:1:daemon:/usr/sbin:/bin/falsebin:x:2:2:bin:/bin:/bin/falsesys:x:3:3:sys:/dev:/bin/falsesync:x:4:65534:sync:/bin:/bin/syncgames:x:5:60:games:/usr/games:/bin/falseman: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 : -troot     x  0  0    root    /root           /bin/bashdaemon   x  1  1    daemon  /usr/sbin       /bin/falsebin      x  2  2    bin     /bin            /bin/falsesys      x  3  3    sys     /dev            /bin/falsesync     x  4  655  sync    /bin            /bin/syncgames    x  5  60   games   /usr/games      /bin/falseman      x  6  12   man     /var/cache/man  /bin/false

The column command can also be used to view a Comma-Separated Values CSV file:

$ cat contatos.csv | column -t -s,NOME          TEL             EMAILUira Ribeiro  (31) 3069-8315  [email protected]

This can be useful for viewing this type of file without needing to have Excel or similar software.

In some distributions the column command allows you to export 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,NOME,HOME,SHELL{   "table": [      {"login": "root", "password": "x", "uid": "0", "guid": "0", "nome": "root", "home": "/root", "shell": "/bin/bash"},      {"login": "bin", "password": "x", "uid": "1", "guid": "1", "nome": "bin", "home": "/bin", "shell": "/sbin/nologin"},      {"login": "daemon", "password": "x", "uid": "2", "guid": "2", "nome": "daemon", "home": "/sbin", "shell": "/sbin/nologin"},      {"login": "adm", "password": "x", "uid": "3", "guid": "4", "nome": "adm", "home": "/var/adm", "shell": "/sbin/nologin"},      {"login": "lp", "password": "x", "uid": "4", "guid": "7", "nome": "lp", "home": "/var/spool/lpd", "shell": "/sbin/nologin"},      {"login": "sync", "password": "x", "uid": "5", "guid": "0", "nome": "sync", "home": "/sbin", "shell": "/bin/sync"},      {"login": "shutdown", "password": "x", "uid": "6", "guid": "0", "nome": "shutdown", "home": "/sbin", "shell": "/sbin/shutdown"},      {"login": "halt", "password": "x", "uid": "7", "guid": "0", "nome": "halt", "home": "/sbin", "shell": "/sbin/halt"},      {"login": "mail", "password": "x", "uid": "8", "guid": "12", "nome": "mail", "home": "/var/spool/mail", "shell": "/sbin/nologin"},   ]}

This command can be used together 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 ARQUIVO --table-name ARQUIVOS{   "ARQUIVOS": [      {"arquivo": "agradecimento.html"},      {"arquivo": "agradecimentos"},      {"arquivo": "amazon.php"},      {"arquivo": "backup.sh"},      {"arquivo": "certbot-auto"},      {"arquivo": "certificado"},      ]}

In this example, the output of the ls command, with the -1 option that lists only files one per line was transformed by the column command into a JSON file with a column called ARQUIVO, in the ARQUIVOS table.

Learn much more about Linux in our online course. You can enroll 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

Uira Ribeiro

Prof. Uirá Ribeiro

Chair do Board do Linux Professional Institute

After years of watching professionals waste hours on simple tasks because they didn't know the right Linux tools, I decided to teach.
Today I'm the author of most of LPI's official Learning Materials and mentor to over 14,000 certified students.

See my full story →

That was just one piece. The course teaches the whole path — from your first command to exam day.

Certificação Linux — a plataforma líder em certificações Linux no Brasil
WhatsApp