Text Processing (Grep/Sed/Awk)
Instant Column Arithmetic with Awk
🧩 The Challenge
You have a raw data file or command output containing numbers in a specific column, and you need to calculate their total sum quickly without writing a script.
💡 The Fix
Use awk to iterate through the target column, add the values to a variable, and print the final result after the file has been processed.
awk '{sum += $2} END {print sum}' data.txt
⚙️ Why It Works
The code block executes the addition for every line’s second column and the END statement ensures the total is only printed after all lines are parsed.
🚀 Pro-Tip: Use the -F flag to change the field separator if your data is comma-separated instead of whitespace-delimited.
Linux Tips & Tricks | © ngelinux.com | 7/6/2026
