Awk

From Attie's Wiki
(Difference between revisions)
Jump to: navigation, search
m
m
Line 21: Line 21:
 
         END{printf "\n"}' | \
 
         END{printf "\n"}' | \
 
   bc
 
   bc
 +
</source>
 +
===With out tr, cut or bc===
 +
<source lang="bash">
 +
find . -type f -name '*.c' -exec ls -l {} \; | \
 +
  awk 'BEGIN{bytes=0}
 +
            {bytes+=$5}
 +
        END{printf "%d bytes\n", bytes}'
 
</source>
 
</source>
  

Revision as of 15:58, 6 March 2012

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 | \
    wc -l;
done | \
  awk 'BEGIN{lines=0}
            {lines+=$1}
         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

With out 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
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox