0
0
Linux CLIscripting~20 mins

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

Choose your learning style9 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.