terminal.cab

Linux Basics Lesson 12 of 38

guestLevel 1Badges and progress0/100 XP, XP to the next levelStreak0

Evaluating columns with awk

8 minutes

Learning goals

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:

  • -F, columns are separated by a comma, by default by spaces
  • $1, $NF the first and the last column
  • END {...} runs after the last line

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 rotated

in the glossary: awk

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

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

Predict the output

Command

awk 'END {print NR}' logs/app.log

Before the command, in your home folder ~

  • ~/logs/app.log
What does awk 'END {print NR}' logs/app.log print?

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

  1. Read the next line and split it at the comma
  2. If it fits, add column 3 to s
  3. Check the condition NR>1
  4. After the last line, print s

Next up: an API under load, and you put everything from this module to work.

Quiz

  1. Question 1 of 3Which column does $NF stand for?

    Check works once you have chosen an answer.

  2. Question 2 of 3Why does the awk program belong in single quotes?

    Check works once you have chosen an answer.

  3. Question 3 of 3You move a one-liner from Debian to Rocky Linux. What changes for the commands of this lesson?

    Check works once you have chosen an answer.

Cards

  1. Card 1 of 3

    Tap the card or press Space to turn it over.

  2. Card 2 of 3

    Tap the card or press Space to turn it over.

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

Next: lesson 13, Who is hammering the API? (9 min)

What you can do now: awk '{print $1}' logs/app.log

+0 XP in this lesson

  1. Lesson 1: open
  2. Lesson 2: open
  3. Lesson 3: open
  4. Lesson 4: open
  5. Lesson 5: open
  6. Lesson 6: open
  7. Lesson 7: open
  8. Lesson 8: open
  9. Lesson 9: open
  10. Lesson 10: open
  11. Lesson 11: open
  12. Lesson 12: done
  13. Lesson 13: open
  14. Lesson 14: open
  15. Lesson 15: open
  16. Lesson 16: open
  17. Lesson 17: open
  18. Lesson 18: open
  19. Lesson 19: open
  20. Lesson 20: open
  21. Lesson 21: open
  22. Lesson 22: open
  23. Lesson 23: open
  24. Lesson 24: open
  25. Lesson 25: open
  26. Lesson 26: open
  27. Lesson 27: open
  28. Lesson 28: open
  29. Lesson 29: open
  30. Lesson 30: open
  31. Lesson 31: open
  32. Lesson 32: open
  33. Lesson 33: open
  34. Lesson 34: open
  35. Lesson 35: open
  36. Lesson 36: open
  37. Lesson 37: open
  38. Lesson 38: open
1 of 38

Next: lesson 13, Who is hammering the API? (9 min) That's enough for today

Path complete

You finished all 38 lessons of Linux Basics.

Back to the path

Cheat sheet

The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.