Awk pattern scanning and processing language. It is small, fast, and simple. It has a clean syntax and most useful for text processing. awk has built-in variables that are automatically set. For example, $0 variable holds the entire current input line. In this example, print hello world using awk:
echo 'Hello world' | awk '{ print $0 }'
Sample outputs:
Hello world
In this following example, pass two values to awk to print addition:
echo 3 4 | awk '{ print $1 + $2 }'
Sample outputs:
7
To print third word from input, enter:
echo This is a test |awk '{print $3}'
Sample outputs:
a
Printing a text file
Create a file called foo.txt:
$ cat foo.txt
Sample outputs:
Holding on to anger is like grasping a hot coal with the intent of throwing it at someone else; you are the one who gets burned. In a controversy the instant we feel anger we have already ceased striving for the truth, and have begun striving for ourselves.
To print the entire file line by line, enter:
awk '{ print }' foo.txt
OR
awk '{ print $0 }' foo.txt
awk define and print variable
Create a variable called x and y:
awk 'BEGIN{x=3; y=4;}END{ print "x=" x " and y=" y}'</dev/null
To print total of x and y, enter:
echo|awk 'BEGIN{x=3; y=4; total=0}{ total= x+y}END{ print x " + " y " = " total }'
Sample outputs:
7
Say hello to printf
To format and print use a printf statement. The printf works like c printf.
echo Total 5000.5686 | awk '{ printf "%s $%.2f\n", $1, $2 }'
Sample outputs:
Total $5000.57
See awk man page for more details:
$ man awk
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 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 }