Challenge - 5 Problems
Awk Field 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
What is the output of this command?
awk '{print $2}' data.txtLinux CLI
awk '{print $2}' data.txtAttempts:
2 left
💡 Hint
Remember, $2 means the second field in each line.
✗ Incorrect
The awk command prints the second field ($2) from each line. The second fields are 10, 20, and 30.
💻 Command Output
intermediate2:00remaining
What does this awk command print?
Given the same
What is the output of this command?
data.txt file:apple 10 banana 20 cherry 30
What is the output of this command?
awk '{print $1 "-" $2}' data.txtLinux CLI
awk '{print $1 "-" $2}' data.txtAttempts:
2 left
💡 Hint
The command joins the first and second fields with a dash.
✗ Incorrect
The print statement concatenates $1, a dash, and $2, producing lines like apple-10.
📝 Syntax
advanced2:00remaining
Which awk command correctly prints the third field?
Choose the correct awk command to print the third field from a file.
Attempts:
2 left
💡 Hint
Field variables in awk use $ followed by the field number without brackets or signs.
✗ Incorrect
Only $3 is valid syntax to print the third field. $[3] and $-3 are invalid. The semicolon in A is allowed but option C is simpler and correct.
🔧 Debug
advanced2:00remaining
Why does this awk command produce an error?
Consider this command:
Given
Why does it produce an error or unexpected output?
awk '{print $1 + $2}' data.txtGiven
data.txt contains:apple 10 banana 20 cherry 30
Why does it produce an error or unexpected output?
Linux CLI
awk '{print $1 + $2}' data.txtAttempts:
2 left
💡 Hint
Think about how awk treats strings and numbers in arithmetic.
✗ Incorrect
Awk tries to convert $1 and $2 to numbers for addition. Since $1 is 'apple' (a string), it converts to 0, so output is 0+10=10 for first line, which may be unexpected.
🚀 Application
expert2:00remaining
How many lines will this awk command print?
Given
What is the number of lines printed by this command?
data.txt contains:apple 10 banana 20 cherry 30 42 100
What is the number of lines printed by this command?
awk '$1 ~ /^[a-z]+$/ {print $0}' data.txtLinux CLI
awk '$1 ~ /^[a-z]+$/ {print $0}' data.txtAttempts:
2 left
💡 Hint
The regex matches lines where the first field is only lowercase letters.
✗ Incorrect
The first three lines have first fields 'apple', 'banana', 'cherry' which match the regex. The last line '42' does not match, so 3 lines print.