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}'
categorized
for i in `find -type f -name '*.c'`; do cat $i | tr -d ' \t' done | \ awk 'BEGIN{lines=0; empty=0; pp=0; comment=0} { lines++; if ($0 == "") empty++; if (substr($0,0,1) == "#") pp++; if (substr($0,0,2) == "//") comment++; } END{ printf " total lines: %d\n", lines; printf " empty: %d\n", empty; printf "preprocessor: %d\n", pp; printf " c++ comment: %d\n", comment; printf " remain: %d\n", lines - empty - pp - comment; }'
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}'
With 'auto-ranging'
ls -l `find . -name '*.c'` | \ awk 'BEGIN{i=0} {i+=$5} END{f = 0; while (i > 1024) { i /= 1024; f++ } printf "%0.2f ", i if (f == 0) print "bytes"; else if (f == 1) print "kB"; else if (f == 2) print "MB"; else if (f == 3) print "GB"; else if (f == 4) print "TB"; else print "x10^" f }'
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