Challenge - 5 Problems
Awk Patterns Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output of this awk command?
Given a file
What is the output of this command?
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.txtAttempts:
2 left
💡 Hint
Look for lines where the first word is 'apple' and print the second word.
✗ Incorrect
The pattern '$1 == "apple"' selects lines where the first field is 'apple'. The action prints the second field, which are 10 and 40.
💻 Command Output
intermediate2:00remaining
What does this awk script print?
Consider the file
What is the output of:
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.txtAttempts:
2 left
💡 Hint
Check which numbers are greater than 15 and double them.
✗ Incorrect
Numbers greater than 15 (20 and 25) are doubled (40 and 50). Others print as is.
📝 Syntax
advanced2: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'.
Attempts:
2 left
💡 Hint
Use the correct operator for regex matching in awk.
✗ Incorrect
The operator '~' is used for regex matching. '$1 ~ /^a/' matches lines where first field starts with 'a'.
💻 Command Output
advanced2:00remaining
What is the output of this awk script with multiple actions?
Given
What does this command print?
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.txtAttempts:
2 left
💡 Hint
Sum the second field values only for lines where first field is 'pen'.
✗ Incorrect
The script sums 5 and 7 from lines with 'pen' and prints 12 at the end.
🚀 Application
expert2:00remaining
How many lines does this awk command print?
Given
How many lines will this command print?
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
Attempts:
2 left
💡 Hint
Count lines containing 'error' and second field equals 404.
✗ Incorrect
Lines 1 and 5 match both conditions, so 2 lines are printed.