Bird
Raised Fist0
Bash Scriptingscripting~20 mins

awk field extraction in scripts in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Awk Field Extraction Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
What is the output of this awk command?
Given a file 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.txt
Bash Scripting
awk '{print $2}' data.txt
A
10
5
20
B
apple
banana
cherry
C
red
yellow
red
D
1
2
3
Attempts:
2 left
💡 Hint
Remember, awk fields are numbered starting from 1, and $2 means the second word in each line.
💻 Command Output
intermediate
1:30remaining
Which awk command extracts the third field from a CSV file?
You have a CSV file 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?
Aawk -F':' '{print $3}' records.csv
Bawk '{print $3}' records.csv
Cawk -F',' '{print $2}' records.csv
Dawk -F',' '{print $3}' records.csv
Attempts:
2 left
💡 Hint
CSV fields are separated by commas, so set the field separator to a comma.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in this awk script snippet
What is wrong with this awk command?
awk '{print $1 $2}' file.txt

It is intended to print the first and second fields separated by a space.
AMissing comma between $1 and $2 inside print
BNo error, command is correct
CIncorrect use of single quotes
DMissing semicolon after print statement
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.
🚀 Application
advanced
2:00remaining
Extract and sum the second field values using awk
Given a file numbers.txt with lines:
a 3
b 7
c 10

Which awk command sums all values in the second field and prints the total?
Aawk '{print sum + $2}' numbers.txt
Bawk '{sum = $2} END {print sum}' numbers.txt
Cawk '{sum += $2} END {print sum}' numbers.txt
Dawk 'END {print sum}' numbers.txt
Attempts:
2 left
💡 Hint
Use a variable to accumulate the sum inside the main block, then print it in the END block.
🔧 Debug
expert
2:30remaining
Why does this awk command fail to extract the fourth field?
You run this command:
awk '{print $4}' file.txt

But 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?
AThe file uses commas as separators but awk expects spaces by default
BThe file uses tabs as separators but awk expects spaces by default
CThe file lines have trailing spaces causing $4 to be empty
DThe file uses multiple spaces or tabs, causing fields to be miscounted
Attempts:
2 left
💡 Hint
Check what character separates fields in the file and if awk is set to use it.

Practice

(1/5)
1. What does awk '{print $2}' file.txt do in a bash script?
easy
A. Prints the second word (field) from each line of file.txt
B. Prints the entire line from file.txt
C. Prints the first word (field) from each line of file.txt
D. Prints the number of lines in file.txt

Solution

  1. Step 1: Understand awk field numbering

    Awk splits each line into fields separated by spaces by default. $1 is the first field, $2 is the second, and so on.
  2. Step 2: Analyze the command

    The command prints $2, which is the second field of each line in file.txt.
  3. Final Answer:

    Prints the second word (field) from each line of file.txt -> Option A
  4. Quick Check:

    Awk $2 prints second field [OK]
Hint: Remember $1 is first field, $2 second, etc. [OK]
Common Mistakes:
  • Confusing $2 with line number
  • Thinking it prints entire line
  • Mixing up field numbers starting at 0
2. Which of the following is the correct syntax to print the third field using awk with a comma as the field separator?
easy
A. awk -F ',' '{print $3}' file.csv
B. awk '{print $3 -F ','}' file.csv
C. awk '{print $3, -F ","}' file.csv
D. awk -F ';' '{print $3}' file.csv

Solution

  1. Step 1: Identify field separator option

    The -F option sets the field separator. For comma-separated files, use -F ','.
  2. Step 2: Check print syntax

    The correct print syntax is '{print $3}' to print the third field.
  3. Final Answer:

    awk -F ',' '{print $3}' file.csv -> Option A
  4. Quick Check:

    Use -F ',' before script block [OK]
Hint: Use -F 'separator' before the awk script block [OK]
Common Mistakes:
  • Placing -F inside the print statement
  • Using wrong separator like semicolon for CSV
  • Misplacing quotes around separator
3. Given a file data.txt with the following content:
apple 10 red
banana 5 yellow
cherry 20 red

What is the output of awk '{print $1, $3}' data.txt?
medium
A. apple 10 banana 5 cherry 20
B. apple red banana yellow cherry red
C. 10 red 5 yellow 20 red
D. apple banana cherry

Solution

  1. Step 1: Identify fields in each line

    Each line has three fields: $1=fruit, $2=number, $3=color.
  2. Step 2: Print first and third fields

    The command prints $1 and $3 separated by space, so output lines are fruit and color pairs.
  3. Final Answer:

    apple red banana yellow cherry red -> Option B
  4. Quick Check:

    Print $1 and $3 fields [OK]
Hint: Match $1, $2, $3 to columns in data [OK]
Common Mistakes:
  • Printing wrong fields like $2 instead of $3
  • Confusing field order
  • Expecting numbers instead of colors
4. What is wrong with this awk command to print the first field from a colon-separated file?
awk '{print $1}' -F ':' file.txt
medium
A. The colon separator is invalid in awk
B. The $1 should be $0 to print the whole line
C. The -F option should come before the script block
D. The file name should come before the script block

Solution

  1. Step 1: Check placement of -F option

    The -F option must come before the script block in awk commands.
  2. Step 2: Analyze the command structure

    Correct syntax is: awk -F ':' '{print $1}' file.txt
  3. Final Answer:

    The -F option should come before the script block -> Option C
  4. Quick Check:

    -F before script block [OK]
Hint: Put -F before the '{print ...}' block [OK]
Common Mistakes:
  • Placing -F after the script block
  • Using wrong field variable
  • Misordering file name and script
5. You have a file records.txt with lines like:
John|Doe|30|Engineer
Jane|Smith|25|Designer
Bob|Brown|40|Manager

Write an awk command to print only the names (first and last) of people older than 28.
hard
A. awk -F '|' '$3 < 28 {print $1, $2}' records.txt
B. awk -F ',' '$3 > 28 {print $1, $2}' records.txt
C. awk '{if ($3 > 28) print $1, $2}' records.txt
D. awk -F '|' '$3 > 28 {print $1, $2}' records.txt

Solution

  1. Step 1: Set correct field separator

    The file uses '|' as separator, so use -F '|' to split fields correctly.
  2. Step 2: Filter by age and print names

    Use condition $3 > 28 to select lines where age is greater than 28, then print first and second fields (first and last names).
  3. Final Answer:

    awk -F '|' '$3 > 28 {print $1, $2}' records.txt -> Option D
  4. Quick Check:

    Filter $3 > 28 and print $1, $2 [OK]
Hint: Use -F '|' and condition $3 > 28 to filter [OK]
Common Mistakes:
  • Using wrong field separator
  • Using wrong comparison operator
  • Not filtering before printing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard