AWK

AWK

Show ASCII table

# Usage: awk -f showascii
BEGIN {
    for (i=0; i<16; i++) {
      for (j=32+i; j<128; j+=16) {
        if (j == 32) { x = "SPC" }
        else if (j == 127) { x = "DEL" }
        else { x = sprintf("%c",j) }
        printf("%3d: %-5s",j,x)
      }
      print ""
    }
}

Tally up a column of figures

#! /bin/sh
case "$1" in 
[1-9]*) colnum="$1"; shift;; 
*) echo "Usage: `basename $0` colnum [files]" 1>&2; exit 1;; 
esac 
awk '{sum += $col} 
END {print sum}' col=$colnum OFMT='%.2f' ${1+"$@"}

Use awk to list the thirty most-common words in a text file

#Usage: awk -f wordfreq textfile.txt

{   nbytes += length($0) + 2 # +2 for CR/LF
    nfields += NF
    $0 = tolower($0)
    for (i=1; i<=NF; i++) {
      arr[$i]++
    }
}
END {
    show = (show == "") ? 30 : show
    width1 = length(show)
    PROCINFO["sorted_in"] = "@val_num_desc"
    for (i in arr) {
      if (width2 == 0) { width2 = length(arr[i]) }
      if (n++ >= show) { break }
      printf("%*d %*d %s\n",width1,n,width2,arr[i],i)
    }
    printf("input: %d records, %d bytes, %d words of which %d are unique\n",NR,nbytes,nfields,length(arr))
    exit(0)
}

Print lines with a unique first field

$ awk '!x[$1]++' ana5.txt
abby baby 
abeam ameba 
abed bade bead 
abel able bale bela elba 
abet bate beat beta 
abets baste beast beats betas 
abetter beretta 
abhorred harbored 
abhorring harboring 
abhors hasbro 
abides biased 
able abel bale bela elba 
abler blare 
ablest bleats stable tables 
aboard abroad baroda 
abode adobe 
abridge brigade 
abridges brigades 
abroad aboard baroda 
abuse beaus 
abut batu tabu 
abuts tubas 
accouterments accoutrements 
accoutrements accouterments 
accrual caracul


Print a count of words by ending character

$ awk 'length > 1{++a[substr(tolower($0), length)]}END{for (k in a) print a[k], k}' /usr/share/dict/words | sort -n
1 j
2 á
2 q
29 é
45 v
132 z
159 u
172 b
195 x
196 f
254 w
479 p
492 i
760 o
797 c
816 k
957 m
1033 h
1753 a
2103 l
4304 r
4465 t
4513 n
5631 y
7090 g
7412 e
8064 d
50493 s