About awk.info
» table of contents
» featured topics
» page tags
|
|
|
|
|
|
Mar 01: Michael Sanders demos an X-windows GUI for AWK.
Mar 01: Awk100#24: A. Lahm and E. de Rinaldis' patent search, in AWK
Feb 28: Tim Menzies asks this community to write an AWK cookbook.
Feb 28: Arnold Robbins announces a new debugger for GAWK.
Feb 28: Awk100#23: Premysl Janouch offers a IRC bot, In AWK
Feb 28: Updated: the AWK FAQ
Feb 28: Tim Menzies offers a tiny content management system, in Awk.
Jan 31: Comment system added to awk.info. For example, see discussion bottom of ?keys2awk
Jan 31: Martin Cohen shows that Gawk can handle massively long strings (300 million characters).
Jan 31: The AWK FAQ is being updated. For comments/ corrections/ extensions, please mail tim@menzies.us
Jan 31: Martin Cohen finds Awk on the Android platform.
Jan 31: Aleksey Cheusov released a new version of runawk.
Jan 31: Hirofumi Saito contributes a candidate Awk mascot.
Jan 31: Michael Sanders shows how to quickly build an AWK GUI for windows.
Jan 31: Hyung-Hwan Chung offers QSE, an embeddable Awk Interpreter.
by R. Loui
Here are a few short programs that do the same thing in each language. When reading these examples, the question to ask is `how many language features do I need to understand in order to understand the syntax of these examples'.
Some of these are longer than they need to be since they don't exploit some (e.g.) command line trick to wrap the code in for each line do X. And that is the point- for teach-ability, the preferred language is the one you need to know LESS about before you can be useful in it.
hello world
PERL:
print "hello world\n"
GAWK:
BEGIN { print "hello world" }
One plus one
PERL
$x= $x+1;
GAWK
x= x+1
Printing
PERL
print $x, $y, $z;
GAWK
print x,y,z
Printing the first field in a file
PERL
while (<>) {
split(/ /);
print "@_[0]\n"
}
GAWK
{ print $1 }
Printing lines, reversing fields
PERL
while (<>) {
split(/ /);
print "@_[1] @_[0]\n"
}
GAWK
{ print $2, $1 }
Concatenation of variables
PERL
command = "cat $fname1 $fname2 > $fname3"
GAWK
command = "cat " fname1 " " fname2 " > " fname3
Looping
PERL:
for (1..10) { print $_,"\n" }
GAWK:
BEGIN {
for (i=1; i<=10; i++) print i
}
Pairs of numbers
PERL:
for (1..10) { print "$_ ",$_-1 }
print "\n"
GAWK:
BEGIN {
for (i=1; i<=10; i++) printf i " " i-1
print ""
}
List of words into a hash
PERL
foreach $x ( split(/ /,"this is not stored linearly") )
{ print "$x\n" }
GAWK
BEGIN {
split("this is not stored linearly",temp)
for (i in temp) print temp[i]
}
Printing a hash in some key order
PERL
$n = split(/ /,"this is not stored linearly");
for $i (0..$n-1) { print "$i @_[$i]\n" }
print "\n";
for $i (@_) { print ++$j," ",$i,"\n" }
AWK
BEGIN {
n = split("this is not stored linearly",temp)
for (i=1; i<=n; i++) print i, temp[i]
print ""
for (i in temp) print i, temp[i]
}
Printing all lines in a file
PERL
open file,"/etc/passwd";
while (<file>) { print $_ }
GAWK
BEGIN {
while (getline < "/etc/passwd") print
}
Printing a string
PERL
$x = "this " . "that " . "\n"; print $x
GAWK
BEGIN {
x = "this " "that " "\n" ; printf x
}
Building and printing an array
PERL
$assoc{"this"} = 4;
$assoc{"that"} = 4;
$assoc{"the other thing"} = 15;
for $i (keys %assoc) { print "$i $assoc{$i}\n" }
GAWK
BEGIN {
assoc["this"] = 4
assoc["that"] = 4
assoc["the other thing"] = 15
for (i in assoc) print i,assoc[i]
}
Sorting an array
PERL
split(/ /,"this will be sorted once in an array");
foreach $i (sort @_) { print "$i\n" }
GAWK
BEGIN {
split("this will be sorted once in an array",temp," ")
for (i in temp) print temp[i] | "sort"
while ("sort" | getline) print
}
Sorting an array (#2)
GAWK
BEGIN {
split("this will be sorted once in an array",temp," ")
n=asort(temp)
for (i=1;i<=n;i++) print temp[i]
}
Print all lines, vowels changed to stars
PERL
while (<STDIN>) {
s/[aeiou]/*/g;
print $_
}
GAWK
{gsub(/[aeiou]/,"*"); print }
Report from file
PERL
#!/pkg/gnu/bin/perl
# this is a comment
#
open(stream1,"w | ");
while ($line = <stream1>) {
($user, $tty, $login, $junk) = split(/ +/, $line, 4);
print "$user $login ",substr($line,49)
}
GAWK
#!/pkg/gnu/bin/gawk -f
# this is a comment
#
BEGIN {
while ("w" | getline) {
user = $1; tty = $2; login = $3
print user, login, substr($0,49)
}
}
Web Slurping
PERL
open(stream1,"lynx -dump 'cs.wustl.edu/~loui' | ");
while ($line = <stream1>) {
if ($flag && $line =~ /[0-9]/) { print $line }
if ($line =~ /References/) { $flag = 1 }
}
GAWK
BEGIN {
com = "lynx -dump 'cs.wustl.edu/~loui' &> /dev/stdout"
while (com | getline line) {
if (flag && line ~ /[0-9]/) { print line }
if (line ~ /References/) { flag = 1 }
}
}
blog comments powered by Disqus