[1]
awk 'BEGIN{
stock["orange"] = 100
stock["apple"] = 200
stock["grapes"] = 30
stock["pineapple"] = 10
for ( i in stock )
if ( stock[i] > 50 ) printf "%-13s%3d\n", i, stock[i]
}' < /dev/null
[2]
#!/bin/sh
awk '{
fruit[$1] += $2
print "We have", fruit[$1], $1
}
END{
for (i in fruit) {
print "Summary:"
print i, fruit[i]
}
}'
[3]
The backslash at the end of each line is tcsh.
It is not needed if you put the script in a file.
#/bin/sh
# Script for extracting information from a table
# for COMP111 Lab session
#
# By Chan Kam Fai
awk '\
BEGIN { \
FS = ":" \
# Count["M"] = 0; \
# Count["F"] = 0; \
} \
\
{ Name[NR] = $1; \
ID[NR] = $2; \
# printf "|%s|\n", $3 \
Count[$3] += 1; \
Dept[NR] = $4; \
Year[NR] = $5; \
} \
\
END { \
for (i = 1; i <= 3; i++) { \
printf "Year %d\n\n", i \
printf "Name ID Dept\n" ; \
printf "-------------------- -------- ----\n" ; \
for (j = 1; j <= NR; j++) \
if (Year[j] == i) \
printf "%-20s %s %s\n", Name[j], ID[j], Dept[j] \
printf "\n========================================\n\n" \
} \
printf "Some Statistics:\n\n" ; \
printf "Number of male students : %d\n", Count["M"] ; \
printf "Number of female students : %d\n", Count["F"] ; \
} '