How do I add two numbers using awk? How do I calculate all incoming number stream on fly and display the total using awk?
AWK is a programming language designed for processing text-based data, either in files or data streams. Calculating incoming stream is not a big deal with awk. You can add two numbers as follows:
# add 2 + 5 echo |awk '{ print 2+3 }' # add incoming 10 + 10 echo 10 | awk '{ print $1 + 10}'
Create a text file called numbers as follows /tmp/numbers:
10 20 30
To add numbers, enter:
awk '{total += $1}END{ print total}' /tmp/numbers
Sample outputs:
60
Use the ps command output (stream) to calculate total size of php-cgi process using awk, enter:
ps -aylC php-cgi | grep php-cgi | awk '{total += $8}END{size= total / 1024; printf "php-cgi total size %.2f MB\n", size}'
Sample outputs:
php-cgi total size 2004.21 MB
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop













{ 0 comments… add one now }