0
0
Linux CLIscripting~5 mins

awk patterns and actions in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to find and process specific parts of text in files or command output. Awk helps by letting you match patterns and then do actions on those matches, like printing or calculating.
When you want to print only lines containing a certain word from a log file.
When you need to sum numbers in a specific column of a data file.
When you want to extract and reformat parts of text from command output.
When you want to count how many lines match a pattern in a file.
When you want to replace or modify text only on lines that match a pattern.
Commands
This command searches the syslog file for lines containing the word 'error' and prints those entire lines. The pattern is '/error/' and the action is '{print $0}', which means print the whole line.
Terminal
awk '/error/ {print $0}' /var/log/syslog
Expected OutputExpected
Apr 26 10:15:32 server1 kernel: [12345.678901] error: device not found Apr 26 10:16:45 server1 systemd: error: failed to start service
This command reads 'data.txt' and prints the first and third columns only for lines where the third column is greater than 50. The pattern is '$3 > 50' and the action is '{print $1, $3}'.
Terminal
awk '$3 > 50 {print $1, $3}' data.txt
Expected OutputExpected
John 75 Alice 82
This command sums all numbers in the second column of 'numbers.txt'. The BEGIN block initializes sum to 0, the main block adds each second column value to sum, and the END block prints the total sum.
Terminal
awk 'BEGIN {sum=0} {sum += $2} END {print sum}' numbers.txt
Expected OutputExpected
210
This command pipes three fruit names to awk, which prints only lines containing the letter 'a'. It shows how awk can process piped input with a pattern and action.
Terminal
echo -e "apple\nbanana\ncherry" | awk '/a/ {print $0}'
Expected OutputExpected
apple banana
Key Concept

If you remember nothing else from awk patterns and actions, remember: awk looks at each line, checks if it matches the pattern, and if yes, runs the action on that line.

Common Mistakes
Using single quotes inside the awk pattern without escaping
This breaks the shell command because the shell misinterprets the quotes.
Use double quotes for the shell command or escape single quotes inside the awk script.
Forgetting to specify an action, expecting awk to print matching lines automatically
Awk prints the whole line only if no action is given; if you provide an empty action block, nothing prints.
Either omit the action block to print matching lines or explicitly use '{print $0}'.
Using incorrect field numbers like $0 for a specific column
$0 means the whole line, not a column; using it for a column causes wrong output.
Use $1, $2, $3, etc., for columns; use $0 only to refer to the entire line.
Summary
Use awk patterns to select lines based on text or conditions.
Use actions to print, calculate, or modify data on matching lines.
Combine BEGIN and END blocks to initialize and finalize processing.