Awk

From Attie's Wiki
(Difference between revisions)
Jump to: navigation, search
m
m
Line 1: Line 1:
 
This page contains some example awk scripts.
 
This page contains some example awk scripts.
 +
 +
==Reverse fields on a line basis==
 +
<source lang="bash">
 +
echo -e "a.b.c\nd.e\nf.g.h.i" | \
 +
  awk -F . '{
 +
              i=NF;
 +
              do {
 +
                printf $(i);
 +
                if (i>1) {
 +
                  printf FS;
 +
                } else {
 +
                  printf "\n";
 +
                }
 +
              } while(--i > 0);
 +
            }'
 +
</source>
  
 
==Find the total number of lines of source code==
 
==Find the total number of lines of source code==

Revision as of 15:59, 6 May 2012

This page contains some example awk scripts.

Contents

Reverse fields on a line basis

echo -e "a.b.c\nd.e\nf.g.h.i" | \
  awk -F . '{
              i=NF;
              do {
                printf $(i);
                if (i>1) {
                  printf FS;
                } else {
                  printf "\n";
                }
              } while(--i > 0);
            }'

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

Notice the speed difference between this, and the previous example.

ls -l `find . -type f -name '*.c'` | \
  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

Little Helper

When building a large package, you may find that you need to do the same thing to multiple subdirectories. This script can be used in that situation.

script
make -k
^D
cat typescript  | awk -- 'BEGIN{catch=0}{if ($4 == "Error") { catch=1 } else if (catch == 1 && $2 == "Leaving" && $3 == "directory") { print $4; catch = 0}}' | sed -re 's/^`//' -e "s/'.$/"
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox