Thursday, March 23, 2006

AWK, the fun way

#! /bin/sh
#
# the longest word
# in redhat 9, the longest word is 28 alphabets "antidisestablishmentarianism"

gawk '{print length($0),$0}' /usr/share/dict/linux.words | sort -n -k 1 | tail -1




#! /bin/sh
#
# non-repeatable alphabets in word
# in redhat 9,
# 14 alphabets - "ambidextrously"
# 13 alphabets - "consumptively", "copyrightable", "unpredictably"

if [ $# -ne 1 ]; then
 echo "Usage: $0 "
 exit
fi

gawk -v n=$1 '
{
        if ( n!=length($1) ) {
                next;
        }
        delete s;
        split(tolower($1),a,"");
        for(i in a) {
                ind=a[i];
                ++s[ind];
                if (s[ind]>1) next
        }
        print $1
}' /usr/share/dict/linux.words




# /bin/sh
#
# how to use AWK to calculate the min/avg/max/sum
#
# sample output from redhat 9
#     Minimum    = 6
#     Average    = 8.01017
#     Maximum    = 28
#     Sum        = 363878
#     Word Count = 45427

# generate some arbitrary numbers
tmpfile=/tmp/funwithawk.$$
awk '{print length($0)}' /usr/share/dict/linux.words > $tmpfile

# shell functions
sum()
{
        awk '{s+=$1} END {print s}' $1
}
avg()
{
        awk '{s+=$1} END {print s/NR}' $1
}
min()
{
        awk 'BEGIN {min=9999999} {if ($1max) {max=$1}} END {print max}' $1
}
wc()
{
        awk 'END {print NR}' $1
}

echo "Minimum    = `min $tmpfile`"
echo "Average    = `avg $tmpfile`"
echo "Maximum    = `max $tmpfile`"
echo "Sum        = `sum $tmpfile`"
echo "Word Count = `wc $tmpfile`"

rm -f $tmpfile

0 Comments:

Post a Comment

<< Home