|
||||||
This article looks at AWK - a powerful Linux tool that can be used to analyse and format data such as CSV files containing stock quotes from Yahoo! Finance.
If anyone wants one single reason for moving to Linux, then that reason could well be AWK; AWK is a text data processing language and is one of the most powerful tools in the Linux toolbox; and all that a developer has to do in order to use it is to fire up a Linux terminal and start programming. What Exactly is AWK?To say that AWK is a text data processing language is correct, but doesn't fully convey just how powerful and adaptable AWK is. - it can:
and it comes as standard in Linux. Why is AWK Called AWK?Many Linux commands tell a programmer what they're to be used for just by their names (get, find, sort and whois, for example) but awk is not like that: AWK is actually named after its creators - Alfred Aho, Peter Wienberger and Brian Kernighan. How is AWK Pronounced?AWK is not pronounced as three separate letters (A-W-K), but as single word rhyming with Auk ( the Auk is a seabird, and is sometimes used as an emblem for AWK). My System Doesn't Seem to Have AWK InstalledIn most cases a Linux distribution will have an AWK interpreter installed rather than AWK itself, and the interpreter may be:
In fact, even if AWK does seem to be installed then it is probably actually a link to one of the interpreters: $ ls -l $(which awk)
/usr/bin/awk -> /etc/alternatives/awk
$ ls -l /etc/alternatives/awk
/etc/alternatives/awk -> /usr/bin/mawk
The Structure of an AWK ProgramAll AWK programs have the same structure:
The AWK program will also need to be told about:
so, this takes the form of: awk -F<field delimiter> '
BEGIN {initial actions}
pattern 1 {action set 1}
pattern 2 {action set 2}
END {final actions}
' <input file>
An Example of an AWK ProgramAWK is a data processing language and so some data is needed- something like the following stock quote information from the Yahoo! Finance web site: "NOVL",4.37,"10/7/2008","4:00pm",-0.47,4.93,5.00,4.37,5034992
"MSFT",23.23,"10/7/2008","4:00pm",-1.68,25.00,25.21,23.14,144142064
"HOLL",2.07,"10/7/2008","3:59pm",+0.47,1.42,2.32,1.42,91085
If this data (from http://finance.yahoo.com/q/cq?d=v1&s=NOVL,MSFT,HOLL) is saved as quotes.csv then a simple AWK program can be used to create a useful report: awk -F, '
BEGIN {
print "Stock Quote Analysis\n"
inc_count = 0
same_count = 0
dec_count = 0
}
$5 > 0 {inc_count++}
$5 == 0 {same_count++}
$5 < 0 {dec_count++}
{
gsub("\"", "", $1)
printf "%5s %6.2f %6.2f\n",$1,$2,$5
}
END {
print ""
print inc_count" shares have increased"
print dec_count" shares have decreased"
print same_count" shares are unchanged"
}
' quotes.csv
In this example the code:
and the output is: Stock Quote Analysis
NOVL 4.37 -0.47
MSFT 23.23 -1.68
HOLL 2.07 0.47
1 shares have increased
2 shares have decreased
0 shares are unchanged
ConclusionAWK is very simple to use but the program's BEGIN and END sections and a few pattern/action pairs can create a professional report - one that can analyse any data quickly and easily.
The copyright of the article An Introduction to AWK in Linux Programming is owned by Mark Alexander Bain. Permission to republish An Introduction to AWK in print or online must be granted by the author in writing.
Comments
Oct 29, 2008 9:24 PM
Guest :
Aug 19, 2009 12:35 PM
Yuen Kit Mun :
2 Comments
|
||||||
|
|
||||||
|
|
||||||