Admin script has syntax errors...
Posted in 2001
This is a multi-part message in MIME format. ------=_NextPart_000_03CA_01C0759E.442DAAE0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Hello Listers, I downloaded some DBA scripts from the Internet; however, I'm having difficulty with one that supposed to arrange the data in a .unl file to line up the "|" field separators. The "beautify-unl.sh" script is called by another script that creates a ".unl" file with dbspace space information. This ".unl" file is used as an argument to the "beautify-unl.sh" script. I got two errors at first. One had to do with invalid parameters used with grep. I corrected it as follows: if ( echo $1 | grep $delim_pattern ) #if ( echo $1 | grep -q -e $delim_pattern ) The other error has to do with the syntax of awk commands: bbankmain{informix}/sfw/informix>./beautify-unl.sh cco.unl awk: syntax error near line 1 awk: bailing out near line 1 Here is an extract: awk -v myfs="$delim" ' # Inputs a pipe-delimited unl file and outputs it in a format so that # each column maintains its width. The result is that the delimited # columns appear straight and vertical. # BEGIN { ... I would appreciate any help identifying the correct syntax. Regards, Denmark W. "Knowledge is power, but it is only useful if it is shared!" ------=_NextPart_000_03CA_01C0759E.442DAAE0 Content-Type: application/octet-stream; name="beautify-unl.sh" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="beautify-unl.sh" #!/usr/bin/ksh=0A= # beautify-unl.sh - Beautify-UNL files.=0A= #=0A= # Author: Jacob Salomon=0A= # JakeSalomon@netscape.net=0A= # Parameter=0A= # - Delimiter. Optional. Default: The pipe symbol "|". To specify=0A= # the + sign as the delimiter, use -d+=0A= # - Name(s) of .unl file(s) to be alligned along columns.=0A= # Default: stdin=0A= #=0A= # Outputs to stdout.=0A= #=0A= # Release history:=0A= # Release 1.00: 1999-02-25: Initial Release=0A= # Release 1.01: 1999-07-20:=0A= # - Added capability to specify blank delimited files by=0A= # specifying -db option on command line.=0A= # Release 1.10: 2000-04-03=0A= # - Corrected annoying design flaw that assumed the number of fields=0A= # constant in whole file (valid for UNL files, of course).=0A= # Result: A blank first line resulted in blank output. The correc-=0A= # tion displays fields according to the field count on each line.=0A= # - Cleaned up argument-handling code in both shell and awk code.=0A= #--------------------------------------------------------------------=0A= delim_pattern=3D"^-d.$" # Pattern by which I recognize a delimiter=0A= if ( echo $1 | grep $delim_pattern )=0A= #if ( echo $1 | grep -q -e $delim_pattern )=0A= then=0A= echo $1|sed -e s/-d// | read delim # Isolate the delimiter char=0A= delim=3D"@"${delim} # Prepend =3D sign for awk's = sake=0A= shift # Make sure awk can recognize a command=0A= else=0A= delim=3D # Null: Use default delimiter in awk script=0A= fi=0A= #echo "Using delimiter: <$delim>"=0A= # Done with preparation. The awk script below does all the work.=0A= #=0C=0A= awk -v myfs=3D"$delim" '=0A= # Inputs a pipe-delimited unl file and outputs it in a format so that=0A= # each column maintains its width. The result is that the delimited=0A= # columns appear straight and vertical.=0A= #=0A= BEGIN {=0A= if (myfs =3D=3D "") # If no string was passed,=0A= FS =3D "|" # use default delimeter=0A= else=0A= if (myfs =3D=3D "@b") # If used the letter b, means I want to=0A= FS =3D " " # use blank as the delimeter=0A= else # No funny business - use the character=0A= FS =3D substr(myfs,2,1) # specified in the command line.=0A= intpattern =3D "^[0-9]*$" # Integer pattern=0A= decpattern =3D "^[0-9]*\\.[0-9]*$" # Decimal pattern=0A= nupattern =3D intpattern "|" decpattern # General numeric pattern=0A= }=0A= #=0C=0A= # Process each row of input:=0A= # -------------------------=0A= { # For each row, get columns lengths and save data in array.=0A= # No printing here! But check for escaped delimiters and do some=0A= # recombination when they are found.=0A= #=0A= last_char =3D substr($0, length($0), 1)=0A= if (last_char =3D=3D FS) # Ends as expected for UNL file=0A= nfields[NR] =3D NF -1 # Drop field count - UNL formatted=0A= else # Does not end in delimiter - maybe line=0A= nfields[NR] =3D NF # looks like spreadsheet unload=0A= =0A= # Note: Since UNL file ends with delimiter, it could fool awk into=0A= # thinking there is one more field after the delimiter.=0A= # Hence, the subtraction.=0A= =0A= for (lc =3D 1; lc <=3D nfields[NR]; lc++)=0A= {=0A= if (escaped_delim($lc)) # If ends in single backslash=0A= { # squeeze & shift fields=0A= $lc =3D $lc FS $(lc+1) # Reunite fractured column=0A= for (slc =3D lc+1; slc < NF; slc++) # Loop shifts remaining fields=0A= $slc =3D $(slc+1) # down by 1 column position=0A= nfields[NR]-- # Drop field count of this line=0A= }=0A= if (length($lc) > max_length[lc]) # maintain max length for=0A= max_length[lc] =3D length($lc) # this column=0A= =0A= data_field[NR,lc] =3D $lc # Save all data in mXn array=0A= }=0A= }=0A= =0A= END { # Now it is almost safe to print.=0A= for (line_num =3D 1; line_num <=3D NR; line_num++)=0A= {=0A= for (lc =3D 1; lc <=3D nfields[line_num]; lc++) # Loop to print 1 = line=0A= { # 1 field per round=0A= if (data_field[line_num,lc] ~ nupattern) # For numeric data=0A= out_format =3D "%*s" FS # use right-justified format=0A= else # For non-numeric data=0A= out_format =3D "%-*s" FS # use left-justified format=0A= printf(out_format, max_length[lc],=0A= data_field[line_num,lc]); # No new-line after each printf=0A= }=0A= printf("\\n") # Feed line only after outputting whole row=0A= }=0A= } # Close END handler=0A= #=0C=0A= # escaped_delim() - Function to examine a string and determine if it=0A= # ends in a single backslash. If yes, we need to=0A= # take action.=0A= #=0A= # Analysis: If there is a single backslash at the end of the string,=0A= # it was there to escape the field delimiter, which AWK does=0A= # not realize. A double backslash indicates an escape back-=0A= # slash at the end of the field but