Linux Basics Lesson 12 of 38
Evaluating columns with awk
8 minutes
Learning goals
- Pick out columns with awk '{print $1}' and set the separator with -F.
- Print only the lines that fit a condition.
- Add up a column and print the total in END.
Picking out columns
awk splits each line at spaces. $1 is the first column, $2 the second, $NF the last, and $0 the whole line. Several spaces in a
row count as one separator, and that is what makes it better than cut for logs:
awk '{print $1}' logs/app.log
Purpose: Splits every line into columns and lets you print and calculate with them.
Syntax: awk [-F SEP] 'CONDITION {ACTION}' [FILE...]
Options:
Example:
awk '{print $1}' logs/app.log.1
23:40:12
23:45:31
23:52:08
23:59:58
awk '{print $2, $NF}' logs/app.log.1
INFO 77ms
INFO entries
WARN 1320ms
INFO rotatedAnother separator: -F
For a CSV you name the separator with -F. With a condition in front of the braces, only the lines that fit it are processed. NR is
the number of the current line, so NR>1 skips the header:
awk -F, '{print $1, $3}' orders.csv
customer amount
anna 120
ben 80
cem 45
dana 200
eva 60
finn 15
gina 300
hugo 95
awk -F, 'NR>1 {print $2}' orders.csv
Berlin
Hamburg
Berlin
Munich
Berlin
Hamburg
Berlin
Cologne
awk -F, 'NR>1 && $3 > 100 {print $1}' orders.csv
anna
dana
gina
Calculating: END
Variables come into being when you use them, and they start at 0. The action sum += $3 adds up, line by line. The block after END runs
once, after the last line. That is the place for the total:
awk -F, 'NR>1 {sum += $3} END {print sum}' orders.csv
915
awk '/ERROR/ {n++} END {print n}' logs/app.log
2
Practice
Terminal task 1/3
The second column of logs/app.log is the level. Print only that column for every line.
Task 1/3: The second column of logs/app.log is the level. Print only that column for every line.
The second column of logs/app.log is the level. Print only that column for every line.
Task complete
Solution
awk '{print $2}' logs/app.log Why: In this log, there are two spaces behind INFO but only one behind ERROR. awk does not care, and cut would stumble over exactly that.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Terminal task 2/3
Print the customers from orders.csv whose amount (third column) is greater than 100. Only the names, separated by a comma in the file.
Task 2/3: Print the customers from orders.csv whose amount (third column) is greater than 100. Only the names, separated by a comma in the file.
Print the customers from orders.csv whose amount (third column) is greater than 100. Only the names, separated by a comma in the file.
Task complete
Solution
awk -F, 'NR>1 && $3 > 100 {print $1}' orders.csv Why: Without NR>1, the header slips through. amount is no number, so awk compares it as text, and as text it is greater than 100. That is a typical quiet error with CSV files.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Terminal task 3/3
What do all orders in orders.csv add up to? Only the number should be on the screen.
Task 3/3: What do all orders in orders.csv add up to? Only the number should be on the screen.
What do all orders in orders.csv add up to? Only the number should be on the screen.
Task complete
Solution
awk -F, 'NR>1 {sum += $3} END {print sum}' orders.csv Why: The variable starts at 0 and needs no declaration. The header counts as 0 when you add it, so the result is the same here, but the NR>1 says what you mean.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Predict the output
Command
awk 'END {print NR}' logs/app.logBefore the command, in your home folder ~
- ~/logs/app.log
Check works once you have chosen an answer.
Put in order
How does awk work through the line awk -F, 'NR>1 {s += $3} END {print s}' file? Put it in the right order.
Steps
- Read the next line and split it at the comma
- If it fits, add column 3 to s
- Check the condition NR>1
- After the last line, print s
Next up: an API under load, and you put everything from this module to work.
Quiz
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
Cards
-
Card 1 of 3
Tap the card or press Space to turn it over.
-
Card 2 of 3
Tap the card or press Space to turn it over.
-
Card 3 of 3
Tap the card or press Space to turn it over.
End of the lesson
Still open: 5 exercises, 3 quiz questions
What you can do now: awk '{print $1}' logs/app.log
+0 XP in this lesson
- Lesson 1: open
- Lesson 2: open
- Lesson 3: open
- Lesson 4: open
- Lesson 5: open
- Lesson 6: open
- Lesson 7: open
- Lesson 8: open
- Lesson 9: open
- Lesson 10: open
- Lesson 11: open
- Lesson 12: done
- Lesson 13: open
- Lesson 14: open
- Lesson 15: open
- Lesson 16: open
- Lesson 17: open
- Lesson 18: open
- Lesson 19: open
- Lesson 20: open
- Lesson 21: open
- Lesson 22: open
- Lesson 23: open
- Lesson 24: open
- Lesson 25: open
- Lesson 26: open
- Lesson 27: open
- Lesson 28: open
- Lesson 29: open
- Lesson 30: open
- Lesson 31: open
- Lesson 32: open
- Lesson 33: open
- Lesson 34: open
- Lesson 35: open
- Lesson 36: open
- Lesson 37: open
- Lesson 38: open
Cheat sheet
-
awk '{print $1}' <file>first column, separated by spaces -
awk -F, '{print $2}' <file>second column of a CSV -
awk '$3 > 100' <file>only lines with a condition -
awk '{s += $3} END {print s}' <file>add up a column
The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.
- Download PDF (PDF, 20 KB)
- View as image (PNG, 73 KB)