awk patterns and actions in Linux CLI - Time & Space Complexity
When using awk, we often run commands that check each line of a file and perform actions based on patterns.
We want to understand how the time it takes grows as the file gets bigger.
Analyze the time complexity of the following awk command.
awk '/error/ { print $0 }' logfile.txt
This command reads each line of logfile.txt, checks if it contains the word "error", and prints the line if it does.
- Primary operation: Reading and checking each line of the file.
- How many times: Once for every line in the file.
As the number of lines grows, the command checks each line once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 line checks |
| 100 | 100 line checks |
| 1000 | 1000 line checks |
Pattern observation: The work grows directly with the number of lines.
Time Complexity: O(n)
This means the time to run grows in a straight line as the file gets bigger.
[X] Wrong: "The command only checks lines with 'error', so it runs faster on big files."
[OK] Correct: The command still reads and checks every line, even if it prints only some lines.
Understanding how commands like awk scale helps you write scripts that handle big files efficiently and shows you know how to think about performance.
"What if the awk command had nested loops inside the action? How would the time complexity change?"
Practice
awk, what does a pattern do?Solution
Step 1: Understand the role of patterns in awk
Patterns in awk are used to match lines in the input data. Only lines matching the pattern are processed.Step 2: Differentiate patterns from actions
Actions define what to do with matched lines, but patterns decide which lines to select.Final Answer:
It selects which lines to process -> Option AQuick Check:
Pattern = line selector [OK]
- Thinking patterns change file content
- Confusing patterns with output commands
- Assuming patterns run shell commands
awk?Solution
Step 1: Identify correct awk print syntax
The print statement must be inside braces {} and use $1 to refer to the first field.Step 2: Check each option
awk '{print $1}' file.txt uses correct braces and $1. awk 'print $1' file.txt misses braces. awk '{print 1}' file.txt prints number 1 literally. awk '{print $}' file.txt uses invalid $.Final Answer:
awk '{print $1}' file.txt -> Option DQuick Check:
Print first field = awk '{print $1}' [OK]
- Omitting braces around print
- Using $ without field number
- Printing literal numbers instead of fields
1: apple 10
2: banana 20
3: cherry 30
awk '$2 > 15 {print $1}' file.txt?Solution
Step 1: Understand the pattern condition
The pattern '$2 > 15' means only lines where the second field is greater than 15 are selected.Step 2: Apply the condition to each line
Line 1: 10 > 15? No. Line 2: 20 > 15? Yes. Line 3: 30 > 15? Yes.Step 3: Print the first field of matching lines
For lines 2 and 3, print $1: 'banana' and 'cherry'.Final Answer:
banana cherry -> Option BQuick Check:
Fields > 15 print first field = banana, cherry [OK]
- Printing all lines ignoring pattern
- Printing second field instead of first
- Confusing numeric comparison with string
awk '$1 == "apple" print $2' file.txtSolution
Step 1: Check awk syntax for pattern and action
Awk requires actions to be inside braces {} after the pattern.Step 2: Identify missing braces
The command misses braces around 'print $2', causing syntax error.Final Answer:
Missing braces {} around action -> Option CQuick Check:
Actions need braces {} [OK]
- Omitting braces around actions
- Using wrong comparison operators
- Misquoting strings inside patterns
Solution
Step 1: Understand pattern for fields starting with 'a' or 'b'
The regex /^[ab]/ matches strings starting with 'a' or 'b'. The operator ~ means regex match.Step 2: Check each option's correctness
awk '$1 ~ /^[ab]/ {print $2}' file.txt uses correct regex and syntax. awk '$1 == "a" || $1 == "b" {print $2}' file.txt compares whole field to 'a' or 'b' exactly, not starts with. awk '$1 = "a" || $1 = "b" {print $2}' file.txt uses assignment '=' instead of comparison '=='. awk '$1 ~ /a|b/ {print $2}' file.txt matches anywhere 'a' or 'b' appears, not just start.Final Answer:
awk '$1 ~ /^[ab]/ {print $2}' file.txt -> Option AQuick Check:
Regex match start with a or b = awk '$1 ~ /^[ab]/ {print $2}' file.txt [OK]
- Using = instead of == for comparison
- Matching exact string instead of start
- Using wrong regex without ^ anchor
Ready to prove it?
Pass the test and this pattern moves unknown
