AWK & SED

mit AWK und SED lassen sich unter LINUX zahlreiche automatisierungsaufgaben erledigen

ls -la > temp.txt
awk '/root/ {print $1, $9;}' temp.txt

 

awk ‚  ‚{print $1}‘   file

 

ls files_list | awk ‚{print „mv „$1“ „$1″.new“}‘ | sh

 

 

  1. Renaming within the name:
    ls -1 *old* | awk '{print "mv "$1" "$1}' | sed s/old/new/2 | sh
    (although in some cases it will fail, as in file_old_and_old)
  2. remove only files:
    ls -l * | grep -v drwx | awk '{print "rm "$9}' | sh
    or with awk alone:
    ls -l|awk '$1!~/^drwx/{print $9}'|xargs rm
    Be careful when trying this out in your home directory. We remove files!
  3. remove only directories
    ls -l | grep '^d' | awk '{print "rm -r "$9}' | sh
    or
    ls -p | grep /$ | wk '{print "rm -r "$1}'
    or with awk alone:
    ls -l|awk '$1~/^d.*x/{print $9}'|xargs rm -r
    Be careful when trying this out in your home directory. We remove things!
  4. killing processes by name (in this example we kill the process called netscape):
    kill `ps auxww | grep netscape | egrep -v grep | awk '{print $2}'`
    or with awk alone:
    ps auxww | awk ‚$0~/netscape/&&$0!~/awk/{print $2}‘ |xargs kill
    It has to be adjusted to fit the ps command on whatever unix system you are on. Basically it is: „If the process is called netscape and it is not called ‚grep netscape‘ (or awk) then print the pid“

 

About the syntax of the awk command interpreter itself:

# gawk --help
Usage: gawk [POSIX or GNU style options] -f progfile [--] file ...
        gawk [POSIX or GNU style options] [--] 'program' file ...
POSIX options:          GNU long options:
        -f progfile             --file=progfile
        -F fs                   --field-separator=fs
        -v var=val              --assign=var=val
        -m[fr] val
        -W compat               --compat
        -W copyleft             --copyleft
        -W copyright            --copyright
        -W help                 --help
        -W lint                 --lint
        -W lint-old             --lint-old
        -W posix                --posix
        -W re-interval          --re-interval
        -W source=program-text  --source=program-text
        -W traditional          --traditional
        -W usage                --usage
        -W version              --version

 

AWK Programming

* All this is fun, but each of these examples only seems to nibble away at „coins.txt“. Why not have Awk figure out everything interesting at one time?

The immediate objection to this idea is that it would be impractical to enter a lot of Awk statements on the command line, but that’s easy to fix. The commands can be written into a file, and then Awk can be told to execute the commands from that file as follows:

   awk -f <awk program file name>

Given an ability to write an Awk program in this way, then what should a „master“ „coins.txt“ analysis program do? Here’s one possible output:

  Summary Data for Coin Collection:

     Gold pieces:                   nn
     Weight of gold pieces:         nn.nn
     Value of gold pieces:       n,nnn.nn

     Silver pieces:                 nn
     Weight of silver pieces:       nn.nn
     Value of silver pieces:     n,nnn.nn

     Total number of pieces:        nn
     Value of collection:        n,nnn.nn

The following Awk program generates this information:

   # This is an awk program that summarizes a coin collection.
   #
   /gold/    { num_gold++; wt_gold += $2 }      # Get weight of gold.
   /silver/  { num_silver++; wt_silver += $2 }  # Get weight of silver.
   END { val_gold = 485 * wt_gold;              # Compute value of gold.
         val_silver = 16 * wt_silver;           # Compute value of silver.
         total = val_gold + val_silver;
         print "Summary data for coin collection:";  # Print results.
         printf ("\n");
         printf ("   Gold pieces:                   %2d\n", num_gold);
         printf ("   Weight of gold pieces:         %5.2f\n", wt_gold);
         printf ("   Value of gold pieces:        %7.2f\n",val_gold);
         printf ("\n");
         printf ("   Silver pieces:                 %2d\n", num_silver);
         printf ("   Weight of silver pieces:       %5.2f\n", wt_silver);
         printf ("   Value of silver pieces:      %7.2f\n",val_silver);
         printf ("\n");
         printf ("   Total number of pieces:        %2d\n", NR);
         printf ("   Value of collection:         %7.2f\n", total); }

This program has a few interesting features:

  • Comments can be inserted in the program by preceding them with a „#“.
  • Note the statements „num_gold++“ and „num_silver++“. C programmers should understand the „++“ operator; those who are not can be assured that it simply increments the specified variable by one.
  • Multiple statements can be written on the same line by separating them with a semicolon („;“).
  • Note the use of the „printf“ statement, which offers more flexible printing capabilities than the „print“ statement. „Printf“ has the general syntax:printf(„<format_code>“,<parameters>)There is one format code for each of the parameters in the list. Each format code determines how its corresponding parameter will be printed. For example, the format code „%2d“ tells Awk to print a two-digit integer number, and the format code „%7.2f“ tells Awk to print a seven-digit floating-point number, with two digits to the right of the decimal point.Note also that, in this example, each string printed by „printf“ ends with a „\n“, which is a code for a „newline“ (ASCII line-feed code). Unlike the „print“ statement, which automatically advances the output to the next line when it prints a line, „printf“ does not automatically advance the output, and by default the next output statement will append its output to the same line. A newline forces the output to skip to the next line.

* This program can be stored in a file named „summary.awk“, and invoked as follows:

   awk -f summary.awk coins.txt

The output is:

   Summary data for coin collection:

      Gold pieces:                    9
      Weight of gold pieces:          6.10
      Value of gold pieces:        2958.50

      Silver pieces:                  4
      Weight of silver pieces:       12.50
      Value of silver pieces:       200.00

      Total number of pieces:        13
      Value of collection:         3158.50