Essential AWK One-Liners: A Practical Guide
Introduction to AWK One-Liners
AWK is a powerful text processing tool available on Unix-like systems. Here’s a collection of practical one-liners that can make your text processing tasks easier.
Basic Text Processing
# Print specific fields
awk '{print $1}' file # Print first field
awk '{print $1,$3}' file # Print first and third fields
awk -F: '{print $1}' file # Split on colon and print first field
# Line filtering
awk 'NR < 5' file # Print first 4 lines
awk 'length > 80' file # Print lines longer than 80 characters
awk '/pattern/' file # Print lines matching pattern
Data Analysis
# Basic calculations
awk '{sum += $1} END {print sum}' file # Sum first column
awk '{if(max < $1) max=$1} END {print max}' file # Find maximum
# Average calculation
awk '{sum += $1} END {print sum/NR}' file # Average of first column
Common Use Cases
Log Analysis
awk '/ERROR/ {print $0}' logfile # Find error messages awk '{freq[$1]++} END {for (w in freq) print w, freq[w]}' file # Word frequency
CSV Processing
awk -F, '{print $2}' file.csv # Print second column awk -F, '$3 > 100 {print $1}' file.csv # Filter by value
System Administration
awk '/^[^#]/ {print}' /etc/passwd # Print non-comment lines df -h | awk '/^\/dev/ {print $1,$5}' # Show disk usage
Advanced Techniques
String Manipulation
awk '{gsub(/old/, "new"); print}' # Replace all occurrences
awk '{print toupper($0)}' # Convert to uppercase
Mathematical Operations
awk '{printf "%.2f\n", $1 * 1.15}' # Add 15% to numbers
Additional Resources
Conclusion
AWK remains an essential tool for text processing and data analysis. These one-liners demonstrate its versatility and power in handling various text processing tasks.
Note: The examples above assume Unix-like systems and GNU AWK. Some syntax might vary on different AWK implementations.