Bird
Raised Fist0
Linux CLIscripting~20 mins

awk basics (field processing) in Linux CLI - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Awk Field 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

What is the output of this command?
awk '{print $2}' data.txt
Linux CLI
awk '{print $2}' data.txt
A
apple
banana
cherry
B
10
20
30
C
1
2
3
D
apple 10
banana 20
cherry 30
Attempts:
2 left
💡 Hint
Remember, $2 means the second field in each line.
💻 Command Output
intermediate
2:00remaining
What does this awk command print?
Given the same data.txt file:
apple 10
banana 20
cherry 30

What is the output of this command?
awk '{print $1 "-" $2}' data.txt
Linux CLI
awk '{print $1 "-" $2}' data.txt
A
apple-10
banana-20
cherry-30
B
apple 10
banana 20
cherry 30
C
10-apple
20-banana
30-cherry
D
apple
banana
cherry
Attempts:
2 left
💡 Hint
The command joins the first and second fields with a dash.
📝 Syntax
advanced
2:00remaining
Which awk command correctly prints the third field?
Choose the correct awk command to print the third field from a file.
Aawk '{print $3;}' file.txt
Bawk '{print $[3]}' file.txt
Cawk '{print $3}' file.txt
Dawk '{print $-3}' file.txt
Attempts:
2 left
💡 Hint
Field variables in awk use $ followed by the field number without brackets or signs.
🔧 Debug
advanced
2:00remaining
Why does this awk command produce an error?
Consider this command:
awk '{print $1 + $2}' data.txt

Given 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.txt
ABecause $2 is not a valid field
BBecause awk requires a comma between fields in print
CBecause the file is missing a header line
DBecause $1 is a string and cannot be added to a number directly
Attempts:
2 left
💡 Hint
Think about how awk treats strings and numbers in arithmetic.
🚀 Application
expert
2:00remaining
How many lines will this awk command print?
Given 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.txt
Linux CLI
awk '$1 ~ /^[a-z]+$/ {print $0}' data.txt
A3
B4
C1
D0
Attempts:
2 left
💡 Hint
The regex matches lines where the first field is only lowercase letters.

Practice

(1/5)
1. What does $2 represent in an awk command?
easy
A. The number of fields in the current line
B. The entire current line
C. The first character of the line
D. The second field (word) in the current line

Solution

  1. Step 1: Understand how awk splits lines

    awk automatically splits each input line into fields separated by spaces or tabs.
  2. Step 2: Identify what $2 means

    $2 refers to the second field (word) in the current line.
  3. Final Answer:

    The second field (word) in the current line -> Option D
  4. Quick Check:

    $2 = second field [OK]
Hint: Remember: $1 is first word, $2 second, and so on [OK]
Common Mistakes:
  • Confusing $2 with the whole line ($0)
  • Thinking $2 is the number of fields (NF)
  • Mixing field numbers with character positions
2. Which of the following is the correct syntax to print the third field of each line using awk?
easy
A. awk '{print $3}' filename
B. awk 'print $3' filename
C. awk '{print 3}' filename
D. awk '{print $}' filename

Solution

  1. Step 1: Recall the basic awk print syntax

    The print statement must be inside braces: {print $3}.
  2. Step 2: Check each option

    awk '{print $3}' filename uses correct braces and $3. Options B, C, and D have syntax errors or wrong usage.
  3. Final Answer:

    awk '{print $3}' filename -> Option A
  4. Quick Check:

    Print field 3 with braces [OK]
Hint: Always use braces { } around print commands in awk [OK]
Common Mistakes:
  • Omitting braces around print statement
  • Printing a number instead of a field variable
  • Using incomplete or invalid field syntax
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 red banana yellow cherry red
B. apple banana cherry
C. 10 red 5 yellow 20 red
D. apple 10 banana 5 cherry 20

Solution

  1. Step 1: Identify fields in each line

    Each line has three fields: $1=fruit, $2=number, $3=color.
  2. Step 2: Understand the print command

    awk '{print $1, $3}' prints the first and third fields separated by space.
  3. Final Answer:

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

    Print first and third fields [OK]
Hint: Match $1, $2, $3 to columns in order [OK]
Common Mistakes:
  • Mixing up field numbers
  • Printing wrong fields or all fields
  • Ignoring spaces between printed fields
4. What is wrong with this command: awk '{print $4}' data.txt if data.txt has only 3 fields per line?
medium
A. It prints the third field instead
B. It causes a syntax error
C. It prints an empty line for each input line
D. It prints the entire line

Solution

  1. Step 1: Understand field numbering in awk

    Fields are numbered from 1 to NF (number of fields). If $4 is requested but only 3 fields exist, $4 is empty.
  2. Step 2: Predict output behavior

    Printing $4 prints nothing, resulting in empty lines for each input line.
  3. Final Answer:

    It prints an empty line for each input line -> Option C
  4. Quick Check:

    Missing field prints empty [OK]
Hint: Fields beyond NF print empty, no error [OK]
Common Mistakes:
  • Expecting syntax error for missing fields
  • Thinking it prints last field instead
  • Assuming it prints whole line if field missing
5. You want to print only the lines from file.txt where the second field is greater than 100 using awk. Which command achieves this?
hard
A. awk '{if $2 > 100 print $0}' file.txt
B. awk '$2 > 100 {print $0}' file.txt
C. awk '{print $0 if $2 > 100}' file.txt
D. awk '$2 => 100 {print $0}' file.txt

Solution

  1. Step 1: Understand condition syntax in awk

    Conditions go before the action block. The correct comparison operator is >, not =>.
  2. Step 2: Check each option

    awk '$2 > 100 {print $0}' file.txt uses correct syntax: $2 > 100 {print $0}. awk '{if $2 > 100 print $0}' file.txt misses parentheses and braces. awk '{print $0 if $2 > 100}' file.txt has wrong syntax. awk '$2 => 100 {print $0}' file.txt uses invalid operator =>.
  3. Final Answer:

    awk '$2 > 100 {print $0}' file.txt -> Option B
  4. Quick Check:

    Condition before action, use > operator [OK]
Hint: Put condition before braces, use > not => [OK]
Common Mistakes:
  • Using invalid comparison operators like =>
  • Placing if inside braces without parentheses
  • Omitting braces or condition syntax errors
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