0
0
Linux CLIscripting~10 mins

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

Choose your learning style9 modes available
Concept Flow - awk patterns and actions
Read line from input
Check pattern match?
NoSkip 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.