WEBVTT
Kind: captions
Language: en

00:00:00.000 --> 00:00:06.072
How frustrating is it when you're working with a Linux command and the output is a messy wall of text?

00:00:06.706 --> 00:00:10.469
I mean, sure, we might be able to "grep" our way into finding what we need, 

00:00:10.469 --> 00:00:13.880
but what if we just want to clear up the output a little bit?

00:00:13.880 --> 00:00:18.485
Well, here's an option you might not know about. The "column" command.

00:00:19.119 --> 00:00:24.124
[chiptunes and guitars intro music, yes that's a SID chip]

00:00:24.958 --> 00:00:29.929
Greetings! And welcome to another exciting Lil' Linux Lesson!

00:00:30.163 --> 00:00:33.433
[intro music, synth bells and a beat]

00:00:33.466 --> 00:00:37.804
I'm Veronica, and today we're going to talk about the "column" command.

00:00:38.371 --> 00:00:42.609
"Column" is an incredibly easy tool to columnate lists.

00:00:43.143 --> 00:00:45.845
[perplexed]
Column-ate... is that how I say that?

00:00:46.312 --> 00:00:51.251
And while it's not as fully featured as "awk", which is a whole language in its own right,

00:00:51.684 --> 00:00:57.557
"column" is a great tool to keep handy when you're dealing on-the-fly with clearing up a bunch of disorganized output.

00:00:58.725 --> 00:01:03.930
For instance, here I have a list of packages overdue for update on a test server.

00:01:03.930 --> 00:01:12.705
Now, the file list is alphabetically organized, which is nice enough, but it's kind of annoying to have to page or scroll through all of this output.

00:01:13.306 --> 00:01:18.945
But if I pipe that output into the "column" command, it spits each line into a new column.

00:01:19.412 --> 00:01:24.050
Now it's much easier for me to visually walk through this list.

00:01:25.051 --> 00:01:29.989
By default, the "column" command fills the column before filling the row.

00:01:29.989 --> 00:01:36.429
But if you pass "column" the "-x" option, now the rows are filled before the columns.

00:01:37.363 --> 00:01:45.105
The "column" command will take standard input, such as the piping that I've been doing, but it can also pass a file name as a parameter.

00:01:45.805 --> 00:01:52.946
So here, for example, we can drop the piping and just type "column" and then a space and then the file name.

00:01:53.746 --> 00:01:58.218
That said, you're probably going to be using this more with piped standard input.

00:01:58.218 --> 00:02:01.754
So, for the rest of this video, that's what I'm focusing on.

00:02:02.288 --> 00:02:08.128
A more practical example would be piping the results of a typical utility such as "find."

00:02:09.062 --> 00:02:13.366
Let's say I wanted to know what OpenType fonts I had installed on my system.

00:02:13.867 --> 00:02:20.373
I'd start by navigating to /usr/share/fonts with the "cd" command.

00:02:20.907 --> 00:02:26.146
And then use the "find" command to list out all the OTF files.

00:02:26.913 --> 00:02:31.818
Now, I know I don't have to explain how "cd" and "find" work,

00:02:31.818 --> 00:02:38.324
because I totally made videos about those already, and I'm sure you took the time to watch them.

00:02:38.892 --> 00:02:43.029
The output of the "find" command is another long list here.

00:02:43.496 --> 00:02:48.434
But if I pipe it into "column", it's much cleaner and easier to see.

00:02:49.335 --> 00:02:55.408
Now, for most casual uses, this is probably all you're ever going to do with the "column" command.

00:02:56.376 --> 00:03:03.750
That said, where "column" can really shine is when used with the "-t" option, which unlocks table mode,

00:03:04.050 --> 00:03:07.554
giving you a ton of options for working with more complex data.

00:03:08.354 --> 00:03:11.991
Take the /etc/passwd file as an example.

00:03:12.358 --> 00:03:16.729
When I "cat" out this file, you'll see it's an ugly mess.
[exaggerated "ugh" sound]

00:03:16.729 --> 00:03:24.637
Now, again, we might use "grep" if we know exactly what information were after here, and that's great.

00:03:24.704 --> 00:03:26.272
[intentional silence]

00:03:26.272 --> 00:03:30.343
But what if we don't know exactly what we're after?

00:03:31.077 --> 00:03:35.648
There have been plenty of times as an admin where I've had to take over someone else's setup,

00:03:36.316 --> 00:03:43.656
and "column" is an indispensable tool at helping make complicated output like this a bit more... discoverable.

00:03:44.390 --> 00:03:51.464
So let's pipe /etc/passwd into "column", passing it the "-t" option for table mode.

00:03:52.198 --> 00:03:57.403
We'll also have to specify a delimiter, which the "column" command refers to as a separator,

00:03:57.971 --> 00:04:01.674
and this will tell "column" what parts of the text to break into separate fields.

00:04:02.575 --> 00:04:06.646
We do that with the "-s" option followed by the delimiter in quotes.

00:04:07.447 --> 00:04:13.586
For /etc/passwd, we use a colon as the delimiter, so that's what we're going to pass.

00:04:14.287 --> 00:04:17.924
I don't know about you, but I think that looks a lot neater.

00:04:18.591 --> 00:04:21.828
We can make it even prettier with column headers.

00:04:22.595 --> 00:04:27.467
Column headers are specified with the "-N" option - with the N as upper case.

00:04:28.268 --> 00:04:30.937
Then we pass a comma separated list of headers.

00:04:30.937 --> 00:04:35.475
In this case, USERNAME, PW for password...

00:04:35.575 --> 00:04:39.112
...and yes, they used to store the password here!

00:04:39.345 --> 00:04:46.986
UID for user ID, GUID for group ID, a COMMENT field, a HOME directory field,

00:04:47.320 --> 00:04:50.623
and the INTERPRETER used on login if applicable.

00:04:51.891 --> 00:04:58.064
When I scroll to the top of this neat output, I can see a very nice, neat looking set of columns,

00:04:58.064 --> 00:05:04.904
and now that it's in a neat table, with these neat headers, there's more neat stuff we can do.

00:05:05.238 --> 00:05:08.775
Can you tell I remember the "-N" option as the neat option?

00:05:09.709 --> 00:05:18.685
See, [chuckling] once we add the "-N" option to name each column, we can use additional options to act on specific columns in the table.

00:05:19.619 --> 00:05:23.189
Let's say we want to hide that outdated password column.

00:05:23.189 --> 00:05:28.428
Well, with "-H" - with the H as upper case - we can do that.

00:05:28.695 --> 00:05:33.566
Just add the PW column name after the option to hide it from view.

00:05:34.200 --> 00:05:40.940
And if we want to reorder these columns, we can do that with the "-O" option - with the O as upper case.

00:05:41.908 --> 00:05:47.547
Just rewrite the column names after the "-O" option in the order you want to see them in.

00:05:48.314 --> 00:05:53.486
And of course, there's a bunch of other options from manipulating the specific columns...

00:05:53.486 --> 00:05:56.589
...and as always, the man page is your friend.

00:05:57.657 --> 00:06:03.429
But before I go, I want to talk about one other "kinda important" option, and that's JSON output.

00:06:04.764 --> 00:06:10.803
Let's say you have a bunch of data in one format that you need to put into JSON for another system to ingest.

00:06:10.803 --> 00:06:17.243
I dealt with this while helping customers migrate data out from COBOL systems into web-based applications.

00:06:18.344 --> 00:06:23.950
Well, if you can iterate through your data using the "column" command, turning it into a pretty looking table,

00:06:24.350 --> 00:06:30.823
you can also output it in JSON format with the "-J" flag - with the J being upper case.

00:06:31.391 --> 00:06:40.066
You'll probably want to name the table, though, and you can do that easily with the "-n" option - with the "n" being lower case.

00:06:40.066 --> 00:06:43.002
Then after that, just type the name of the table.

00:06:43.970 --> 00:06:48.508
When the command runs, it outputs clean, machine-friendly JSON.

00:06:49.609 --> 00:06:57.984
And now you can look like a hero, the next time someone needs some JSON from a system that only outputs a CSV.

00:06:57.984 --> 00:07:01.788
Anyway, that's really it for the column command.

00:07:02.622 --> 00:07:11.864
Column is part of the util-linux package and is packaged for BSD, so chances are it's on your system already, or can easily be installed.

00:07:12.632 --> 00:07:17.804
And yes, there's more you can do with the column command that I didn't cover in this video.

00:07:18.371 --> 00:07:23.609
But in my opinion, if you need more than some basic formatting or on-the-fly conversion, 

00:07:23.609 --> 00:07:28.381
I'd really recommend learning something a little bit more robust, like "awk".

00:07:28.381 --> 00:07:32.852
In fact, maybe I should just plan an "AWK for Absolute Beginners" video.

00:07:33.219 --> 00:07:36.556
If that's something you want to see, leave me a comment!
["Mario enters the painting" sound]

00:07:36.656 --> 00:07:42.061
All that aside, it's great having such a quick and powerful tool at your disposal...

00:07:42.061 --> 00:07:46.933
when you're working as a sysadmin, a software dev, or you're just having fun homelabbing.

00:07:47.433 --> 00:07:53.339
We really have so many fantastic options for manipulating data using Linux. 

00:07:53.339 --> 00:07:57.109
Because Linux is awesome! And so are you.

00:07:57.109 --> 00:08:00.112
[child shouting] 
"1, 2, 3, 4!"
[punk outro music plays]

00:08:00.112 --> 00:08:14.760
[punk outro music continues]

00:08:14.760 --> 00:08:18.097
So, should my next "Lil' Linux Lesson" be about "grep" or "rsync"?

00:08:19.332 --> 00:08:22.668
Join the Patreon or Ko-Fi, and vote now!

