Intermediate AWK Guide
Intermediate AWK Guide
when follow this thing ; give me the essential elements included in this syntaxe given to get a
intermediate level in awk command :
`bash
awk 'pattern { action }' input_file`
To reach an intermediate level in `awk`, you need to understand the essential elements in this syntax:
bash
Essential Elements:
1. `pattern` (Optional)
Defines the condition that selects which lines are processed.
Can be a regular expression, comparison, or special pattern (e.g., `BEGIN`, `END`).
If omitted, `{ action }` applies to all lines.
2. `{ action }` (Optional)
Specifies what to do with matching lines.
Can include printing, modifying fields, performing calculations, etc.
If omitted, `awk` prints the whole line by default.
3. `input_file`
The file that `awk` processes.
If omitted, `awk` reads from standard input.
Intermediate-Level Concepts
bash
✅ Built-in Variables:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/27
`NR` (line number), `FNR` (per file line number), `OFS` (output field separator).
bash
✅ Pattern Matching:
Use regex `/pattern/` or conditions `if ($1 == "value")`
bash
bash
`awk 'BEGIN { print "Start Processing" } { print $1 } END { print "Done" }' file.txt
`
✅ Mathematical Operations:
bash
✅ String Manipulation:
`length()`, `toupper()`, `tolower()`, `substr()`
bash
bash
`awk '{ if ($3 > 50) print $1, "Passed"; else print $1, "Failed" }' file.txt
`
bash
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/27
✅ Processing Multiple Files:
bash
Mastering these elements will give you solid intermediate skills in `awk`. 🚀 Let me know if you need
specific examples!
Sure! Here’s a structured list of essential `{ action }` (commands you can use inside the action block)
and patterns (conditions that determine which lines get processed) in `awk`.
bash
bash
bash
Assign a variable:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/27
bash
Accumulate values:
bash
Convert to uppercase/lowercase:
bash
Get substring:
bash
Get length:
bash
Using `if-else`:
bash
`awk '{ if ($3 > 50) print $1, "Pass"; else print $1, "Fail" }' file.txt
`
bash
`awk '{ print $1, ($3 > 50 ? "Pass" : "Fail") }' file.txt
`
1.5 Looping
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/27
bash
`while` loop:
bash
`awk '{ i=1; while (i<=NF) { print $i; i++ } }' file.txt
`
Math functions:
bash
Formatting output:
bash
`awk '{ printf "User: %s, Score: %d\n", $1, $2 }' file.txt
`
bash
bash
bash
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 5/27
`
bash
bash
`FNR` Line number per file `FNR == 1 { print FILENAME }` (first line of each file)
AND (`&&`): Match lines where column 2 > 50 AND column 3 < 100.
bash
bash
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 6/27
NOT (`!`): Match lines where column 2 is NOT "guest".
bash
💡 Summary
To become intermediate in `awk`, focus on: ✔ Mastering `{ action }`: `print`, variables, conditions,
loops, string functions.
✔ Understanding `pattern`: regex, field conditions, built-in variables, logical operators.
✔ Using and
`BEGIN` `END` for setup and summaries.
Sure! Below are practical `awk` examples for each action and pattern discussed.
bash
bash
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 7/27
bash
Prints first and third column, adds "->", and then the last column.
bash
bash
Convert to uppercase/lowercase:
bash
Get substring:
bash
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 8/27
bash
Using `if-else`:
bash
`awk '{ if ($3 > 50) print $1, "Pass"; else print $1, "Fail" }' file.txt
`
bash
`awk '{ print $1, ($3 > 50 ? "Pass" : "Fail") }' file.txt
`
1.5 Looping
bash
bash
`awk '{ i=1; while (i<=NF) { print $i; i++ } }' file.txt
`
Math functions:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 9/27
bash
Formatted output:
bash
`awk '{ printf "User: %s, Score: %d\n", $1, $2 }' file.txt
`
bash
bash
bash
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 10/27
bash
bash
Using `BEGIN`:
bash
Using `END`:
bash
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 11/27
bash
`awk 'BEGIN { sum=0 } { sum+=$3 } END { print "Total:", sum }' file.txt
`
AND (`&&`):
bash
OR (`||`):
bash
NOT (`!`):
bash
💡 Final Takeaways
✔ Use for printing, calculations, loops, and formatting.
✔ Use patterns for filtering lines based on regex, conditions, and built-in variables.
`{ action }`
so give me a data like example and bind it with some exercice to handy
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 12/27
Great idea! Below is a sample dataset along with some practical exercises to help you get hands-on
with `awk`.
txt
bash
bash
Expected Output:
txt
`John 5000
Alice 6000
Bob 5500
Eve 7000
Charlie 6500
Dave 6200
Carol 7200
Frank 5800
Grace 6800
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 13/27
Mallory 5300
`
bash
Expected Output:
txt
bash
Expected Output:
txt
`Eve 7000
Charlie 6500
Carol 7200
Grace 6800
`
Q5: Add a header and footer when displaying names and salaries.
bash
`awk 'BEGIN { print "Employee Salary Report"; print "----------------------" } { print $2, $4 } END
`
Expected Output:
txt
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 14/27
Alice 6000
Bob 5500
Eve 7000
Charlie 6500
Dave 6200
Carol 7200
Frank 5800
Grace 6800
Mallory 5300
----------------------
Report Complete
`
bash
Expected Output:
txt
bash
`awk '$NF == "Inactive" { print $2, "is not currently working." }' employees.txt
`
Expected Output:
txt
bash
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 15/27
Expected Output:
txt
`JOHN
ALICE
BOB
EVE
CHARLIE
DAVE
CAROL
FRANK
GRACE
MALLORY
`
bash
Expected Output:
txt
`Joh
Ali
Bob
Eve
Cha
Dav
Car
Fra
Gra
Mal
`
bash
`awk '{ if ($4 >= 6000) print $2, "is HIGH SALARY"; else print $2, "is LOW SALARY"; }' employees.txt
`
Expected Output:
txt
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 16/27
Mallory is LOW SALARY
`
Q11: Print employees who are Active AND earn more than 6000.
bash
`awk '$4 > 6000 && $5 == "Active" { print $2, "->", $4 }' employees.txt
`
Expected Output:
txt
bash
Expected Output:
txt
`Alice - HR
Bob - IT
Charlie - IT
Dave - HR
Frank - IT
Grace - HR
`
🚀 Bonus Challenge
Try to write an `awk` command that:
✅ Solution:
bash
`awk 'BEGIN { count=0; total_salary=0 } { count++; total_salary+=$4 } END { print "Total Employees:"
`
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 17/27
Expected Output:
txt
`Total Employees: 10
Total Salary: 61300
`
Awesome! Here are some fun and advanced `awk` challenges that will test your skills. 🚀
🔹 Challenge 1: Find the Employee with the Highest Salary
❓ Task:
Find the employee with the highest salary and display:
txt
`awk 'BEGIN { max_salary=0 } { if ($4 > max_salary) { max_salary=$4; name=$2 } } END { print "Highes
`
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 18/27
🔹 Challenge 2: Count Active and Inactive Employees
❓ Task:
Calculate and print:
txt
`Active Employees: X
Inactive Employees: Y
`
`awk 'BEGIN { active=0; inactive=0 } { if ($5 == "Active") active++; else inactive++ } END { print "
`
txt
`HR: X
IT: Y
Sales: Z
`
`awk '{ dept[$3] += $4; count[$3]++ } END { for (d in dept) print d ":", dept[d]/count[d] }' employe
`
✅ Solution:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 19/27
bash
`7200 Carol
7000 Eve
6800 Grace
6500 Charlie
6200 Dave
6000 Alice
5800 Frank
5500 Bob
5300 Mallory
5000 John
`
`awk '{ new_salary = $4 * 1.1; print $2, "New Salary:", new_salary }' employees.txt
`
txt
`[email protected]
[email protected]
[email protected]
`
✅ Solution:
bash
`awk '{ print $2, ":", sprintf("%s", substr("**********", 1, $4/1000)) }' employees.txt
`
🔹 Expected Output:
txt
`John : *****
Alice : ******
Bob : *****
Eve : *******
Charlie : ******
Dave : ******
Carol : ********
Frank : *****
Grace : *******
Mallory : *****
`
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 21/27
🔹 Challenge 9: Identify Duplicate Departments
❓ Task:
Find departments that have more than one employee.
✅ Solution:
bash
`awk '{ dept[$3]++ } END { for (d in dept) if (dept[d] > 1) print d, "has", dept[d], "employees" }'
`
🔹 Expected Output:
txt
🔹 Expected Output:
txt
`101,John,Sales,5000,Active
102,Alice,HR,6000,Active
103,Bob,IT,5500,Inactive
104,Eve,Sales,7000,Active
105,Charlie,IT,6500,Active
106,Dave,HR,6200,Inactive
107,Carol,Sales,7200,Active
108,Frank,IT,5800,Active
109,Grace,HR,6800,Active
110,Mallory,Sales,5300,Inactive
`
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 22/27
✅ Solution:
bash
`awk '{ print $2, "Yearly Increase:"; for (i=1; i<=5; i++) { $4 *= 1.05; print "Year", i, "->", int
`
🚀 What’s Next?
🎯
These advanced challenges will push your `awk` skills to the next level!
Would you like more real-world projects using `awk`? Maybe integrating with bash scripts? 😃
yes
Awesome! Here are real-world projects where `awk` integrates with Bash scripting to solve practical
problems. 🚀
📌 Project 1: Log File Analyzer
💡 Scenario: You're a sysadmin, and you need to extract insights from an Apache web server log file
( ).
`access.log`
txt
🔹 Tasks:
✅ Count the number of requests per IP address.
✅ Identify the most requested page.
✅ Find the number of failed login attempts (HTTP 401).
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 23/27
🔹 Bash Script ( `log_analyzer.sh`) using `awk`:
bash
`#!/bin/bash
LOG_FILE="access.log"
🔹 Output Example:
txt
` 📌
Total Requests per IP:
192.168.1.10 - 1 requests
192.168.1.11 - 1 requests
192.168.1.12 - 1 requests
txt
`ID,Name,Department,Salary
101,John,Sales,5000
102,Alice,HR,6000
103,Bob,IT,5500
104,Eve,Sales,7000
105,Charlie,IT,6500
`
🔹 Tax Rules:
Salary ≤ 5500 → 10% tax
Salary > 5500 → 20% tax
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 24/27
bash
`#!/bin/bash
CSV_FILE="salaries.csv"
🔹 Output Example:
txt
bash
`#!/bin/bash
txt
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 25/27
4, sneakers,Clothing , 80
`
bash
`#!/bin/bash
CSV_FILE="products.csv"
echo " 📌
Normalized Product Data"
awk -F, 'NR==1 { print; next } { gsub(/ /, "", $0); print toupper($2) "," $3 "," $4 }' $CSV_FILE
`
🔹 Output Example:
txt
bash
`#!/bin/bash
echo " 📌
Top 5 Memory-Consuming Processes"
ps aux --sort=-%mem | awk 'NR==1 || NR<=6 {print $1, $2, $3, $4, $11}'
`
🔹 Output Example:
txt
`📌
USER
Top 5 Memory-Consuming Processes
PID %CPU %MEM COMMAND
root 1234 20.5 8.3 /usr/bin/python3
root 5678 10.2 6.7 /usr/bin/java
john 9101 5.1 4.3 /usr/bin/firefox
root 3141 3.7 3.2 /usr/bin/nginx
alice 4159 2.8 2.9 /usr/bin/mysql
`
bash
`#!/bin/bash
🔹 Output Example:
txt
🚀 What’s Next?
These projects blend `awk` with Bash to solve real-world problems.
Would you like a large-scale automation project combining `awk`, `sed`, and `bash scripting`? 😃
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 27/27