sed execution model: for each line in the input file,
sed -e '1,/Subject:/d
s/^[<tab> ]*//
s/[<tab> ]*$//
s/et al\./<I>et al.<\/I>/g
/et$/{
$!N
s/\n[<tab> ]*/\n/
s/et\nal\./<I>&<\/I>/
P
D
}' file.html >newfile.html
awk execution model:
BEGIN {commands executed once before any input is read}
{main input loop executed for each line of input}
END {commands executed once after all input is read}
awk 'BEGIN { FS = " "; ns = 0; total = 0 }
{sum = $2 + $3 + $4
avg = sum / 3
ns++
total += avg
print ns ": " $1, avg }
END { print ns, "students: " total / ns }' scores
Assuming that the file scores contains
Peter 85 90 95 Paul 25 25 50 Mary 100 80 60this awk command generates the output
1: Peter 90 2: Paul 33.3333 3: Mary 80 3 students: 67.7778
Perl programs are very similar to UNIX shell scripts, except that the entire Perl program is compiled before it is executed.
#!/usr/bin/perl
%words = ("fred","camel","barney","llama",
"betty","oyster","wilma","oyster");
print "What is your name? ";
$name = <STDIN>;
chop($name);
$original_name = $name; #save for greeting
$name =~ s/\W.*//; # get rid of everything after first word
$name =~ tr/A-Z/a-z/; # convert everything to lowercase
if ($name eq "randal") {
print "Hello, Randal! How good of you to be here!\n";
} else {
print "Hello, $original_name!\n"; # generic greeting
$secretword = $words{$name}; # get the secret word
if ($secretword eq "") { # oops, not found
$secretword = "groucho"; # sure, why a duck?
}
print "What is the secret word? ";
$guess = <STDIN>;
chop($guess);
while ($guess ne $secretword) {
print "Wrong, try again. What is the secret word? ";
$guess = <STDIN>;
chop($guess);
}
}