What is AWK?
awk
is a powerful text-processing tool in Linux used to manipulate and analyze text files by processing patterns and performing actions.
Basic Syntax
awk 'pattern {action}' file
-
pattern
: The condition to match (optional). -
action
: Commands to execute on matching lines (optional). -
file
: The file to process.
Common Examples
1. Print All Lines
awk '{print}' file.txt
- Outputs all lines in
file.txt
.
2. Print Specific Columns
awk '{print $1, $3}' file.txt
- Prints the 1st and 3rd columns.
3. Match a Pattern
awk '/error/ {print}' log.txt
- Prints lines containing the word
error
.
4. Conditional Filtering
awk '$3 > 100 {print $1, $3}' data.txt
- Prints rows where the 3rd column is greater than 100.
5. Add or Modify Columns
awk '{print $1, $2, $2 + $3}' data.txt
- Adds a column with the sum of the 2nd and 3rd columns.
6. Count Matching Lines
awk '/pattern/ {count++} END {print count}' file.txt
- Counts and prints lines matching
pattern
.
7. Format Output
awk '{printf "Name: %s, Age: %dn", $1, $2}' names.txt
- Formats and prints fields with a custom output.
8. Use Field Separator
awk -F ':' '{print $1, $NF}' /etc/passwd
- Uses
:
as a separator and prints the 1st and last columns.
Key Concepts
Symbol | Meaning |
---|---|
$1 , $2
|
Represents the 1st, 2nd columns (fields). |
NR |
Line number (e.g., NR == 5 matches line 5). |
NF |
Total number of fields in a line. |
FS |
Field Separator (-F or FS="," ). |
BEGIN |
Executes before processing lines. |
END |
Executes after processing all lines. |
Advanced Examples
Print Line Numbers
awk '{print NR, $0}' file.txt
- Prints line numbers alongside the content.
Find Maximum in a Column
awk '$3 > max {max = $3} END {print max}' data.txt
- Finds and prints the maximum value in the 3rd column.
Sum a Column
awk '{sum += $3} END {print sum}' data.txt
- Sums up all values in the 3rd column.
Filter by Field Count
awk 'NF == 3 {print}' file.txt
- Prints lines with exactly 3 fields.
Practice Data
Create a sample.txt
to test commands:
John 25 500
Mary 30 600
Alex 28 450
Tips
- Test commands with
awk
on small files before applying to large ones. - Combine with other commands like
grep
orsed
for complex workflows.
Source link
lol