Archive for April, 2009
Awk by examples

AWK is a language for processing files of text. A file is treated as a sequence of records, and by default each line is a record. Each line is broken up into a sequence of fields, so we can think of the first word in a line as the first field, the second word as the second field, and so on. An AWK program is of a sequence of pattern-action statements. AWK reads the input a line at a time. A line is scanned for each pattern in the program, and for each pattern that matches, the associated action is executed.
Basics
-
Print /etc/mtab (awk reads a line at time and prints it) :
awk '{ print }' /etc/mtab
-
Print /etc/mtab ($0 denotes the whole line) :
awk '{ print $0 }' /etc/mtab
-
List mounted filesystems ($1 denotes the first element of the line) :
awk '{ print $1 }' /etc/mtab
-
List groups (-F chooses the Field Separator) :
awk -F":" '{ print $1 }' /etc/group
-
List groups and id (awk concatenates print() arguments) :
awk -F":" '{ print $1 " " $3 }' /etc/group
-
List groups and id (Nicer format) :
awk -F":" '{ print "group: " $1 "\tid: " $3 }' /etc/group
-
Launching external awk scripts :
# first.awk BEGIN { FS=":" } { print $1 }
awk -f first.awk /etc/mtab
-
List IPv4 addressess :
ifconfig | awk '/inet / { print $2 }'
-
List processes run by root :
# psroot.awk $1 == "root" { printf("ROOT: "); for (i=11; i<=NF; i++) printf("%s ", $i); printf("\n") }
ps au | awk -f psroot.awk
-
Print X warnings :
awk '$1 ~ /(WW)/ { print }' /var/log/Xorg.0.log
-
Print a file removing comments :
awk '! /^#/ { print }' /etc/fstab
-
Print number of files/directories :
ls -lA | awk 'BEGIN{ x=0 } { x=x+1 } END{ print x-1 }'
-
Awk as calculator :
echo | awk '{ print ((2*5^2-1)%7) }'
-
Count empty lines :
# blanklines.awk BEGIN { x=0 } /^$/ { x=x+1 } END { print "I found " x " blank lines :)" }
awk -f blanklines.awk /etc/profile
-
Use regexp as FS (note the difference though) :
echo ' a b c d '| awk -F"[ \t\n]+" '{ print $2 }'
echo ' a b c d '| awk -F" " '{ print $2 }'
-
Understand NR and NF :
# nrnf.awk BEGIN { x=0 } { print "Words on line " NR ": " NF ; x+=NF } END { print "Total lines: " NR ; print "Total words: " x }
awk -f nrnf.awk nrnf.awk
Another Insurance Joke

I find life insurance jokes incredibly funny, probably because they mostly talk about people and their approach and expectations towards death.
Mary was discussing the various aspects and possible outcome of the insurance policy with the clerk at the Insurance Agency.
During the discussion, she asked: “Suppose I take the life insurance for my husband today and tomorrow he dies? What will I get?
The clerk eyed her suspiciously and replied, “Probably a life sentence.”
If you wake up

A life insurance agent named Michael was the top seller with his company for the sixth year and was about to retire. His best friend Scott went out with him for their last drink on company time.
Scott said, “Mike old boy, I’ve asked you the same question for the last six years, and you’ve always refused to answer. Now that you’re retiring, why not tell me your secret of how you sell so many life policies.”
“You’re right, Scott, I do have a secret, and I can’t think of a better person to tell it to.”
Rather excited, Scott blurted, “Well, what is it?”Michael slowly lit his pipe and puffed on it until it was drawing good. He then put his face close to Scott’s and started his tale.
“First, when I’m telling him all the benefits about the policy, and how he should understand that it is his obligation to his family to protect them..”
“Yes, Mike, we all do that,” interjected Scott, “there’s nothing new that you’re telling me!”
“Don’t be so anxious, Scott, let me tell you in my own way.”
“Sorry, Mike..”Mike’s voice became lower so that Scott had to strain his ears to hear above the bar noise. Then he continued.
“Now, all during my sales pitch I interrupt to ask the client if he feels OK. When he says yes, I look at him kinda funny, but I continue on, stopping every few minutes to either look intently at him or ask him if he would like a glass of water or something.”
“That sounds like an interesting approach,” mutters Scott. “Does it usually end up in a sale?”
“To be honest with you, not very often,” answers Mike. “But the next step works almost every time.”“For God’s sake, Mike, stop playing with me. What is the next step?”
“Well, next I say ‘Don’t let me frighten you into making a hasty decision. Sleep on it tonight. IF you wake in the morning, give me a call then and let me know.’”