Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

break down number(9,2) in to seperate variables 1

Status
Not open for further replies.

m14cus

Technical User
Sep 19, 2003
3
US
Hi,

I’m writing a KSH script that will read a table description output from a spool file, break down the datatype information, add a few columns and generate an XML file.

The part I’m stuck on is breaking down the datatype information returned.

In the spool file I have either NUMBER, NUMBER(9,2), VARCHAR2(30). I need to store each of the elements into separate variables.

Using the above info as an example I want NUMBER or VARCHAR2 stored in a variable called DATATYPE, the size 9 or 30 in a variable called SIZE and if there is a decimal 2, I want this stored in a variable called DEC.

I do not need the brackets and there will not always be a size and/or decimal for the number datatype.

Has anyone any ideas how I can break this out?? I’ve tried looking at awk and sed but only know basic stuff.

 
pls post a sample input with all the cases to consider (and the corresponding variable to store the results in).

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
In my message I have illustrated a sample case and the variables to store the results in.

Cases for input could be

number
number(2)
number(2,1)
char(10)
varchar2(30)

The numbers within the bracket could be any number within reason.

PLS see my orig message for variable details and an example.

Thanks
 
something like this should get you started given your sample input stored in a .txt file.

nawk -f m14.awk myTextFile.txt

#----------------- m14.awk
BEGIN {
VAR_datatype=&quot;DATATYPE&quot;
VAR_size=&quot;SIZE&quot;
VAR_dec=&quot;DEC&quot;
datatype=&quot;&quot;
size=&quot;&quot;
dec=&quot;&quot;
FS=&quot;([(]|[)])&quot;
}

function outputVars()
{
printf(&quot;%s=\&quot;%s\&quot;;%s=\&quot;%s\&quot;;%s=\&quot;%s\&quot;\n&quot;, VAR_datatype, datatype, VAR_size, size, VAR_dec, dec);
}

{
datatype=$1
size=&quot;&quot;
dec=&quot;&quot;

if ( NF <= 1 ) {
outputVars();
next;
}
sN=split($2, sA, &quot;,&quot;);
if ( sN == 1 )
size=$2;
else {
size=sA[1];
dec=sA[2]
}
outputVars();
}


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks alot for the quick solution. So that I fully understand what you have written could you give me a brief explanation as to what is happening. My shell scripting is very rusty :(

Thanks again
 
BEGIN {
# shell var names to assign values to
VAR_datatype=&quot;DATATYPE&quot;
VAR_size=&quot;SIZE&quot;
VAR_dec=&quot;DEC&quot;

# initialize variable value to nothing
datatype=&quot;&quot;
size=&quot;&quot;
dec=&quot;&quot;

# set the FieldSeparator to either '(' or ')'
FS=&quot;([(]|[)])&quot;
}

# function to output variable values in format:
# varNAME=varValue
# These will be printed to shell - shell will probably 'eval'
# this AWK invokation
function outputVars()
{
printf(&quot;%s=\&quot;%s\&quot;;%s=\&quot;%s\&quot;;%s=\&quot;%s\&quot;\n&quot;, VAR_datatype, datatype, VAR_size, size, VAR_dec, dec);
}

# main body
{
# value of the 'datatype' is the FIRST field
# NOTE: fields are separated by either '(' or ')'
# Assumption: there always is SOMETHING on the line
datatype=$1

# initilize the values of 'size' and 'dec' to nothing for
# every NEW line/record to be processed
size=&quot;&quot;
dec=&quot;&quot;

# if there is ONLY 1 record [e.g. 'number'] - output
# values and proceed to the next line/record
if ( NF <= 1 ) {
outputVars();
next;
}

# there're some kind of '()' spec for this line [e.g
# 'char(30)' or 'number(10,2)]. Therefore the size/dec
# spec is within '()' and it's the SECOND field in the
# record with '()' separated fields.
#
# Let's split the SECOND field ['$2'] by ',' into sA
# array. sA returns the number of elements in the sA array
sN=split($2, sA, &quot;,&quot;);

# if number of elements in sA is 1, it means there was no
# decimal specficication [no ',']. Assign the whole
# SECOND field to varible 'size'
if ( sN == 1 )
size=$2;
else {
# there WAS a decimal spec.
# The 'size' is stored in the FIRST entry of the sA array
# The 'decimal precision' is stored in the SECOND entry
# of the sA array
size=sA[1];
dec=sA[2]
}
# output variables
outputVars();
}


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
A shell solution:

Code:
#!/bin/ksh
while IFS=&quot;[,()]&quot; read DATATYPE SIZE DEC
do
        echo DATATYPE = $DATATYPE
        echo SIZE = $SIZE
        echo DEC = $DEC
done < inputfile

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top