Awk
From Attie's Wiki
This page contains some example awk scripts.
Contents |
Find the total number of lines of source code
for i in `find -type f -name '*.c'`; do cat $i done | \ awk 'BEGIN{lines=0} {lines++} END{printf "lines: %d\n", lines}'
Calculate size of all source in directory (using ls and bc)
find . -type f -name '*.c' -exec ls -l {} \; | \ tr -s ' ' | \ cut -d ' ' -f 5 | \ awk 'BEGIN{i=0} {if (i) printf "+"; i = 1; printf $1} END{printf "\n"}' | \ bc
Without tr, cut or bc
find . -type f -name '*.c' -exec ls -l {} \; | \ awk 'BEGIN{bytes=0} {bytes+=$5} END{printf "%d bytes\n", bytes}'
8-bit ADC battery voltage reading
Contents of minicom.cap
:
160 159 159 159 158 158 158 158 157 156 157 156 156 156 155 155 155 155 154 154 154 154 154
Command:
cat minicom.cap | sort -r | uniq -c | \ awk 'BEGIN{tot=0; printf "hh:mm\tvoltage\n"} {tot += $1; printf "%2d:%02d\t%.2fv\n", ($1/60), ($1%60), (((3.3 / 256) * $2) * 2)} END{printf "Total: %d:%02d\n", (tot/60), (tot%60)}'
Output:
hh:mm voltage 0:01 4.12v 0:03 4.10v 0:04 4.07v 0:02 4.05v 0:04 4.02v 0:04 4.00v 0:05 3.97v Total: 0:24