Extract Columns of Data from a CSV with awk

Cameron Nokes
InstructorCameron Nokes
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 4 years ago

In this lesson, we'll use the power of awk to select the columns and rows of data that match our condition. We'll pass a , (comma) to the -F flag (the F stands for field separator), to tell awk to split on commas. From there, the world is our oyster and we can tell awk to only return rows that match whatever parameters we pass.

Cameron Nokes: [0:00] Let's look at that file. We can see our column headers here, we have Name, Department, and then Tenure.

[0:06] Let's say we only want to select people in this Engineering department. I'm not going to pipe this time, so let's invoke awk. Because it's CSV file and it's comma separated, not space separated, I have to tell awk to use a different field separator. I'll use the -F flag, and I'm going to pass a comma to it, to say split on commas.

[0:25] If I just want the Engineering department, which is the second column, I'll do $2 == "Engineering", pass my file name here. Cool. Looks like that worked.

[0:36] What if we wanted to select on two conditions, so we could use the familiar && syntax? Let's say I wanted to get people where their Tenure > 2. Cool. I'll just select that one row, because here their Tenure's 1.1, and or works similarly, so we do ||.

[1:00] I'll say, if the Department is Engineering or their Tenure is greater than 6. Cool. It looks like that. It's working. Of course, if we want in, only print a certain column, we could do that.