Bird
Raised Fist0
Linux CLIscripting~10 mins

awk patterns and actions in Linux CLI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - awk patterns and actions
Read line from input
↓
Check pattern match?
No→Skip line
Yes↓
Execute action block
↓
Output result if action prints
↓
Repeat for next line
↓
End of input?
Yes↓
Exit
Awk reads each line, checks if it matches a pattern, runs actions if matched, then moves to next line until input ends.
Execution Sample
Linux CLI
awk '/error/ { print $0 }' logfile.txt
Prints every line from logfile.txt that contains the word 'error'.
Execution Table
StepInput LinePattern Match (/error/)Action ExecutedOutput
1Info: system startedNoNo
2Warning: low disk spaceNoNo
3Error: failed to load moduleYesPrint lineError: failed to load module
4Info: user loginNoNo
5error: disk not foundYesPrint lineerror: disk not found
6End of logNoNo
💡 Reached end of input file, awk stops processing.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6Final
$0 (current line)Info: system startedWarning: low disk spaceError: failed to load moduleInfo: user loginerror: disk not foundEnd of log
Pattern matchedNoNoYesNoYesNo
OutputError: failed to load moduleerror: disk not found
Key Moments - 3 Insights
Why does awk skip some lines without printing anything?
Awk only runs the action block when the pattern matches the line. Lines without the pattern are skipped, as shown in steps 1, 2, 4, and 6 in the execution_table.
What does $0 represent in awk?
$0 holds the entire current input line. In the example, printing $0 outputs the whole line that matched the pattern, as seen in steps 3 and 5.
Can awk run actions without a pattern?
Yes, if no pattern is given, awk runs the action on every line. Here, the pattern '/error/' filters lines, so only matching lines trigger the action.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of $0 at step 3?
AError: failed to load module
BInfo: system started
CWarning: low disk space
Derror: disk not found
💡 Hint
Check the 'Input Line' column at step 3 in the execution_table.
At which step does the pattern '/error/' first match?
AStep 2
BStep 3
CStep 5
DStep 6
💡 Hint
Look at the 'Pattern Match' column to find the first 'Yes'.
If the pattern was changed to '/Warning/', which step would produce output?
AStep 3
BStep 5
CStep 2
DStep 6
💡 Hint
Look for the line containing 'Warning' in the 'Input Line' column.
Concept Snapshot
awk pattern-action basics:
- awk reads input line by line
- For each line, it checks if the pattern matches
- If yes, it runs the action block
- $0 is the whole current line
- Without a pattern, action runs on all lines
- Useful for filtering and processing text streams
Full Transcript
Awk works by reading each line from input. For every line, it checks if the pattern matches. If the pattern matches, awk runs the action block. For example, the pattern '/error/' matches lines containing 'error'. When matched, the action '{ print $0 }' prints the whole line. Lines without the pattern are skipped and produce no output. The variable $0 holds the entire current line. This process repeats until the end of the input file. This way, awk filters and processes text easily.

Practice

(1/5)
1. In awk, what does a pattern do?
easy
A. It selects which lines to process
B. It changes the file content permanently
C. It saves the output to a file
D. It runs a shell command

Solution

  1. 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.
  2. Step 2: Differentiate patterns from actions

    Actions define what to do with matched lines, but patterns decide which lines to select.
  3. Final Answer:

    It selects which lines to process -> Option A
  4. Quick Check:

    Pattern = line selector [OK]
Hint: Patterns filter lines before actions run [OK]
Common Mistakes:
  • Thinking patterns change file content
  • Confusing patterns with output commands
  • Assuming patterns run shell commands
2. Which of these is the correct syntax to print the first field of each line using awk?
easy
A. awk '{print $}' file.txt
B. awk 'print $1' file.txt
C. awk '{print 1}' file.txt
D. awk '{print $1}' file.txt

Solution

  1. Step 1: Identify correct awk print syntax

    The print statement must be inside braces {} and use $1 to refer to the first field.
  2. 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 $.
  3. Final Answer:

    awk '{print $1}' file.txt -> Option D
  4. Quick Check:

    Print first field = awk '{print $1}' [OK]
Hint: Always use braces {} around actions in awk [OK]
Common Mistakes:
  • Omitting braces around print
  • Using $ without field number
  • Printing literal numbers instead of fields
3. What is the output of this command on a file with lines:
1: apple 10
2: banana 20
3: cherry 30

awk '$2 > 15 {print $1}' file.txt?
medium
A. apple
B. banana cherry
C. 10 20 30
D. apple banana cherry

Solution

  1. Step 1: Understand the pattern condition

    The pattern '$2 > 15' means only lines where the second field is greater than 15 are selected.
  2. Step 2: Apply the condition to each line

    Line 1: 10 > 15? No. Line 2: 20 > 15? Yes. Line 3: 30 > 15? Yes.
  3. Step 3: Print the first field of matching lines

    For lines 2 and 3, print $1: 'banana' and 'cherry'.
  4. Final Answer:

    banana cherry -> Option B
  5. Quick Check:

    Fields > 15 print first field = banana, cherry [OK]
Hint: Check pattern filters before printing fields [OK]
Common Mistakes:
  • Printing all lines ignoring pattern
  • Printing second field instead of first
  • Confusing numeric comparison with string
4. Identify the error in this awk command:
awk '$1 == "apple" print $2' file.txt
medium
A. Incorrect field number
B. Wrong comparison operator
C. Missing braces {} around action
D. Quotes around apple should be single quotes

Solution

  1. Step 1: Check awk syntax for pattern and action

    Awk requires actions to be inside braces {} after the pattern.
  2. Step 2: Identify missing braces

    The command misses braces around 'print $2', causing syntax error.
  3. Final Answer:

    Missing braces {} around action -> Option C
  4. Quick Check:

    Actions need braces {} [OK]
Hint: Always put actions inside {} in awk [OK]
Common Mistakes:
  • Omitting braces around actions
  • Using wrong comparison operators
  • Misquoting strings inside patterns
5. You want to print the second field only for lines where the first field starts with 'a' or 'b'. Which awk command achieves this?
hard
A. awk '$1 ~ /^[ab]/ {print $2}' file.txt
B. awk '$1 == "a" || $1 == "b" {print $2}' file.txt
C. awk '$1 = "a" || $1 = "b" {print $2}' file.txt
D. awk '$1 ~ /a|b/ {print $2}' file.txt

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    awk '$1 ~ /^[ab]/ {print $2}' file.txt -> Option A
  4. Quick Check:

    Regex match start with a or b = awk '$1 ~ /^[ab]/ {print $2}' file.txt [OK]
Hint: Use regex ^[ab] to match start of field [OK]
Common Mistakes:
  • Using = instead of == for comparison
  • Matching exact string instead of start
  • Using wrong regex without ^ anchor
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