Challenge - 5 Problems
Awk Field Extraction Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate1:30remaining
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 red banana 5 yellow cherry 20 red
What is the output of this command?
awk '{print $2}' data.txtBash Scripting
awk '{print $2}' data.txtAttempts:
2 left
💡 Hint
Remember, awk fields are numbered starting from 1, and $2 means the second word in each line.
✗ Incorrect
The command prints the second field ($2) from each line. The second words are 10, 5, and 20.
💻 Command Output
intermediate1:30remaining
Which awk command extracts the third field from a CSV file?
You have a CSV file
Which command correctly prints the third field (age) from each line?
records.csv with lines like:John,Doe,30 Jane,Smith,25 Bob,Brown,40
Which command correctly prints the third field (age) from each line?
Attempts:
2 left
💡 Hint
CSV fields are separated by commas, so set the field separator to a comma.
✗ Incorrect
Using -F',' sets the field separator to comma, so $3 is the third field (age).
📝 Syntax
advanced1:30remaining
Identify the syntax error in this awk script snippet
What is wrong with this awk command?
It is intended to print the first and second fields separated by a space.
awk '{print $1 $2}' file.txtIt is intended to print the first and second fields separated by a space.
Attempts:
2 left
💡 Hint
In awk, print $1 $2 prints the fields separated by the output field separator (OFS), which defaults to a single space.
✗ Incorrect
There is no syntax error. The command correctly prints the first field, a space, and the second field using the default OFS.
🚀 Application
advanced2:00remaining
Extract and sum the second field values using awk
Given a file
Which awk command sums all values in the second field and prints the total?
numbers.txt with lines:a 3 b 7 c 10
Which awk command sums all values in the second field and prints the total?
Attempts:
2 left
💡 Hint
Use a variable to accumulate the sum inside the main block, then print it in the END block.
✗ Incorrect
Option C correctly adds each $2 to sum and prints sum after processing all lines.
🔧 Debug
expert2:30remaining
Why does this awk command fail to extract the fourth field?
You run this command:
But it prints empty lines even though the file has data.
The file lines look like:
What is the most likely reason?
awk '{print $4}' file.txtBut it prints empty lines even though the file has data.
The file lines look like:
one,two,three,four,five
What is the most likely reason?
Attempts:
2 left
💡 Hint
Check what character separates fields in the file and if awk is set to use it.
✗ Incorrect
If fields are separated by commas, awk needs -F',' to split correctly. Without it, $4 is empty.