What is awk?
awk is a scripting language (command) for efficiently extracting, processing, and aggregating text data in Linux/Unix environments.
Reference: GNU awk
What This Article Covers
This article explains the awk command divided into the following 8 categories:
- Conditional Branching & Control Structures
- Functions & Processing
- String Processing & Regular Expressions
- Fields & Records
- Arrays & Data Structures
- Variables & Scripts
- Comparison & Integration with Other Commands
- Data Splitting
Each section starts with code and output to verify basic behavior, making it easy to confirm how things work.
Follow the links for more detailed behavior.
Conditional Branching & Control Structures
Basic Syntax of awk if
Create File
cat << 'EOF' > input.txt
apple 100
banana 200
cherry 150
EOF
Command
awk '{ if ($2 > 150) print $1, $2 }' input.txt
Output
banana 200
Details of awk if
A Beginner’s Guide to Text Processing using awk and if
Basic Syntax of awk begin
Create File
cat << 'EOF' > input.txt
apple 100
banana 200
orange 300
EOF
Command
awk 'BEGIN { print "=== Start Processing ===" }
{ print "Item:", $1, "Price:", $2 }
END { print "=== End Processing ===" }' input.txt
Output
=== Start Processing ===
Item: apple Price: 100
Item: banana Price: 200
Item: orange Price: 300
=== End Processing ===
Details of awk begin
The Ultimate Guide to AWK BEGIN Blocks: From Variable Initialization to Aggregation Patterns
Functions & Processing
Basic Syntax of awk function
Create File
cat << 'EOF' > input.txt
apple 100
banana 200
orange 150
EOF
Command
awk '
function add_tax(price) {
return price * 1.1
}
{
taxed = add_tax($2)
print $1, taxed
}
' input.txt
Output
apple 110
banana 220
orange 165
Details of awk function
A Practical Guide to Understanding awk Functions and Achieving Efficient Text Processing
Basic Syntax of awk printf
Create File
cat << 'EOF' > input.txt
apple 100
banana 200
orange 150
EOF
Command
awk '{printf "%s : %d\n", $1, $2}' input.txt
Output
apple : 100
banana : 200
orange : 150
Details of awk printf
Mastering Data Formatting with awk and printf: A Beginner’s Guide
Basic Syntax of awk getline
Create File
cat << 'EOF' > input.txt
line1
line2
line3
EOF
Command
awk '{
print "Current line:", $0
if (getline next_line) {
print "getline result:", next_line
}
}' input.txt
Output
Current line: line1
getline result: line2
Current line: line3
Details of awk getline
A Practical Guide to Understanding awk getline and Its Safe Usage
String Processing & Regular Expressions
Basic Syntax of awk match
Create File
cat << 'EOF' > input.txt
Hello 123 World
abc456def
no numbers here
EOF
Command
awk '{ if (match($0, /[0-9]+/)) print $0, "-> RSTART=" RSTART ", RLENGTH=" RLENGTH }' input.txt
Output
Hello 123 World -> RSTART=7, RLENGTH=3
abc456def -> RSTART=4, RLENGTH=3
Details of awk match
Mastering awk’s match Function: A Practical Approach
Basic Syntax of awk sub
Create File
cat << 'EOF' > input.txt
apple orange apple
banana apple grape
EOF
Command
awk '{ sub("apple","APPLE"); print }' input.txt
Output
APPLE orange apple
banana APPLE grape
Details of awk sub
Mastering the sub Function in awk: A Beginner’s Guide
Basic Syntax of awk gsub
Create File
cat << 'EOF' > input.txt
apple orange apple grape
EOF
Command
awk '{ gsub("apple", "banana"); print }' input.txt
Output
banana orange banana grape
Details of awk gsub
Mastering awk gsub: A Beginner’s Guide to String Replacement from Basics to Advanced
Basic Syntax of awk substr
Create File
cat << 'EOF' > input.txt
Hello,awk,substr,function
EOF
Command
awk '{ print substr($0,1,5) }' input.txt
Output
Hello
Details of awk substr
Mastering the awk substr Function: A Hands-on Guide
Basic Syntax of awk Regular Expressions
Create File
cat << 'EOF' > input.txt
apple 123
banana 456
cherry_789
grape-001
EOF
Command
awk '/[a-z]+ [0-9]+/' input.txt
Output
apple 123
banana 456
Details of awk Regular Expressions
A Beginner’s Guide to awk and Regular Expressions
Fields & Records
Basic Syntax of awk nf
Create File
cat << 'EOF' > input.txt
apple orange banana
cat dog
red blue green yellow
EOF
Command
awk '{print NF}' input.txt
Output
3
2
4
Details of awk nf
Mastering AWK and NF: The Practical Guide to Seamless Data Processing
Basic Syntax of awk nr
Create File
cat << 'EOF' > input.txt
apple
banana
cherry
EOF
Command
awk '{print NR, $0}' input.txt
Output
1 apple
2 banana
3 cherry
Details of awk nr
Mastering awk: An Introductory Guide to NR and Row Manipulation
Basic Syntax of awk fs
Create File
cat << 'EOF' > input.txt
apple,100,red
banana,200,yellow
grape,300,purple
EOF
Command
awk -F',' '{print $1, $2, $3}' input.txt
Output
apple 100 red
banana 200 yellow
grape 300 purple
Details of awk fs
AWK FS Fundamentals: Essential and Applied Techniques
Basic Syntax of awk ofs
Create File
cat << 'EOF' > input.txt
apple orange banana
dog cat mouse
EOF
Command
awk 'BEGIN{OFS=","} {print $1, $2, $3}' input.txt
Output
apple,orange,banana
dog,cat,mouse
Details of awk ofs
Mastering Delimiters with awk and OFS
Basic Syntax of awk field separator
Create File
cat << 'EOF' > input.txt
apple orange banana
cat dog mouse
one two three
EOF
Command
awk '{print $1, $2, $3}' input.txt
Output
apple orange banana
cat dog mouse
one two three
Details of awk field separator
Mastering awk Fields and Separators: A Practical Introduction
Arrays & Data Structures
Basic Syntax of awk array
Create File
cat << 'EOF' > input.txt
apple 100
banana 200
apple 150
orange 300
banana 50
EOF
Command
awk '{ arr[$1] += $2 } END { for (key in arr) print key, arr[key] }' input.txt
Output
apple 250
banana 250
orange 300
Details of awk array
AWK Arrays: A Beginner’s Guide to the Fundamentals
Variables & Scripts
Basic Syntax of awk variable
Create File
cat << 'EOF' > input.txt
apple 100
banana 200
orange 300
EOF
Command
awk '{ total += $2 } END { print total }' input.txt
Output
600
Details of awk variable
AWK Variables Explained: A Beginner’s Guide to the Essentials
Basic Syntax of awk script
Create File
cat << 'EOF' > input.txt
apple
banana
cherry
EOF
Create File
cat << 'EOF' > script.awk
BEGIN { print "=== awk script start ===" }
{ print NR ":" $0 }
END { print "=== awk script end ===" }
EOF
Command
awk -f script.awk input.txt
Output
=== awk script start ===
1:apple
2:banana
3:cherry
=== awk script end ===
Details of awk script
Mastering Text Processing: Learning awk and Scripting
Comparison & Integration with Other Commands
Basic Syntax of awk sed
Create File
cat << 'EOF' > input.txt
apple 100
banana 200
orange 300
EOF
Command
awk '{ print $1, $2 }' input.txt
Output
apple 100
banana 200
orange 300
Command
sed 's/ / : /' input.txt
Output
apple : 100
banana : 200
orange : 300
Details of awk sed
Text Processing Basics with awk and sed
Basic Syntax of awk grep
Create File
cat << 'EOF' > input.txt
apple 100
banana 200
orange 150
apple 300
grape 120
EOF
Command
awk '{print $1, $2}' input.txt
Output
apple 100
banana 200
orange 150
apple 300
grape 120
Command
grep "apple" input.txt
Output
apple 100
apple 300
Details of awk grep
Mastering awk and grep: A Practical Foundation Guide
Basic Syntax of awk sort
Create File
cat << 'EOF' > input.txt
apple 300
orange 150
banana 200
grape 180
apple 120
EOF
Command
awk '{ print $1, $2 }' input.txt
Output
apple 300
orange 150
banana 200
grape 180
apple 120
Command
sort input.txt
Output
apple 120
apple 300
banana 200
grape 180
orange 150
Details of awk sort
Mastering awk and sort: A Beginner’s Guide
Data Splitting
Basic Syntax of awk split
Create File
cat << 'EOF' > input.txt
apple,orange,banana
EOF
Command
awk '{
split($0, fruits, ",")
print fruits[1]
print fruits[2]
print fruits[3]
}' input.txt
Output
apple
orange
banana
Details of awk split
Mastering the awk split Function: Explained Simply for New Users
Summary
This article explained the awk command divided into the following 8 categories:
- Conditional Branching & Control Structures
- Functions & Processing
- String Processing & Regular Expressions
- Fields & Records
- Arrays & Data Structures
- Variables & Scripts
- Comparison & Integration with Other Commands
- Data Splitting
We comprehensively covered awk's fundamental design through advanced text manipulation and integration with other commands, showcasing its powerful data processing capabilities.
awk is not merely a text filter — it is a highly complete "programming language" equipped with control structures, dynamic arrays, and a rich set of built-in functions.
By applying the techniques introduced in each section, you will be able to efficiently handle everyday troubleshooting tasks — such as aggregating bloated logs or performing complex data splits — with a single-line script (one-liner).
Other Command Articles
We also cover articles on commands beyond awk.
Feel free to take a look.