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 3 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.

~ an hour ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today