Linux Terminal Command: awk
The awk command is an essential tool in Text Processing & Piping. In this tutorial, we will explore what awk does, look at everyday examples, and cover advanced options to supercharge your command-line workflow.
Concept & Explanation
The awk command is a full programming language designed for data extraction and report generation, processing files line by line using fields.
Common Options & Syntax
awk [options] [arguments]
Here are the most common flags used with awk:
- Simple Usage: Basic default commands.
- Detailed View: Shows diagnostic information.
- Advanced Actions: Can chain parameters for scripting.
1. Interactive Example (Simple)
Here is how most people run the command:
# Example
awk '{print $1}' access.log
What it does: Extracts and prints the first field (column) of every line in ‘access.log’.
2. Power-User Example (Advanced)
For scripting and advanced diagnostics, use this configuration:
# Advanced
awk -F',' '$3 > 100 {sum += $3; count++} END {print "Avg:", sum/count}' data.csv
What it does: Defines comma as separator (-F), reads the third field, calculates the average if the value exceeds 100, and prints the result.
⚙️ Warning & Common Pitfalls
[!WARNING]
awkfields start indexing at 1 ($1,$2), while$0represents the entire raw line.
🔗 Related Commands
Here are some related posts on cli_tty1 you might want to check out:
- at : Queue, examine or delete jobs for later execution (one-off).