0
0
Linux CLIscripting~20 mins

awk patterns and actions in Linux CLI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Awk Patterns Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this awk command?
Given a file data.txt with the following content:
apple 10
banana 20
cherry 30
apple 40

What is the output of this command?
awk '$1 == "apple" {print $2}' data.txt
A
10
40
B
apple
apple
C
20
30
D
10
20
30
40
Attempts:
2 left
💡 Hint
Look for lines where the first word is 'apple' and print the second word.
💻 Command Output
intermediate
2:00remaining
What does this awk script print?
Consider the file numbers.txt with content:
5
10
15
20
25

What is the output of:
awk '{if ($1 > 15) print $1 * 2; else print $1}' numbers.txt
A
5
10
15
80
100
B
10
20
30
40
50
C
5
10
15
20
25
D
5
10
15
40
50
Attempts:
2 left
💡 Hint
Check which numbers are greater than 15 and double them.
📝 Syntax
advanced
2:00remaining
Which awk command correctly prints the second field only for lines where the first field starts with 'a'?
Choose the correct awk command to print the second field only if the first field starts with the letter 'a'.
Aawk '$1 =~ /^a/ {print $2}' file.txt
Bawk '$1 == /^a/ {print $2}' file.txt
Cawk '$1 ~ /^a/ {print $2}' file.txt
Dawk '$1 = /^a/ {print $2}' file.txt
Attempts:
2 left
💡 Hint
Use the correct operator for regex matching in awk.
💻 Command Output
advanced
2:00remaining
What is the output of this awk script with multiple actions?
Given items.txt content:
pen 5
pencil 3
eraser 2
pen 7

What does this command print?
awk '$1 == "pen" {sum += $2} END {print sum}' items.txt
A12
B
5
7
Cpenpen
D0
Attempts:
2 left
💡 Hint
Sum the second field values only for lines where first field is 'pen'.
🚀 Application
expert
2:00remaining
How many lines does this awk command print?
Given log.txt with:
error 404
info start
error 500
warning low
error 404

How many lines will this command print?
awk '/error/ && $2 == 404' log.txt
A3
B2
C1
D0
Attempts:
2 left
💡 Hint
Count lines containing 'error' and second field equals 404.