[Previous] [TOC] [Next]

Awk: Transformation example

Goal: Generate DCL procedure to add users to the system using information provided in a list.

Input file

The SAS System         13:18 Monday, March 17, 1997   3

           OBS     NAME     VORNAME    ABT         GEB

            1     Zinser    Martin     CS     06/03/66
            2     Pyne      Alan       Mar    17/03/68

Code

BEGIN { group=100;
        uid=7;
        while ($1!~/OBS/){getline};
        getline;
        print "$mcr authorize";
      }
{gsub(/\//,"_",$5);
 print "copy default", $2, "/owner=\"" $2 ",", $3 "\"-"; 
 print " /device=" $4 "$root: -"; 
 printf " /directory=[%s]/uic=[%o,%o]-", $2, group, uid; 
 print " /password=x" $5 "x";
 uid++;}        
END {print "exit";} 

Result

$mcr authorize
copy default Zinser /owner="Zinser, Martin"-
 /device=CS$root: -
 /directory=[Zinser]/uic=[144,7]-
 /password=x06_03_66x
copy default Pyne /owner="Pyne, Alan"-
 /device=Mar$root: -
 /directory=[Pyne]/uic=[144,10]-
 /password=x17_03_68x
exit

Explanation

while ...
All common control structures available
getline;
Explicit input of the next line from the input file
gsub(/\//,"_",$5);
Replace all /-signs in the birthdate with _
printf " /directory=[%s]/uic=[%o,%o]-", $2, group, uid
Print with format instructions. Here:
  • %s: Print a character string
  • %o: Print a number in octal format (Group and Userid, sic!)


Martin P.J. Zinser