Create table


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create table
# 15  
Old 05-06-2010
Hi Aigless,

Thanks a lot for your great help .Its working fine.


Regards,
MR
# 16  
Old 05-13-2010
Hi Aigles,

Thanks for your help ,script is working fine.I need some additional modifcation on the script

If string data lenghth below 50 ,set string default length to 50 ,if between 50 to lessthan 100,set default data length to 100
If between 100 to less than 200 ,set default length to 500

If between 500 to less than 1000,set default length 1000.

Numbers data length ,no issue ,set to 38 whatvery length comes

If there is spaces or no value at the last column ,script to remove those values .
Code:
ex:
col1|col2|col3|col4|
xx|xx1|5566|8999|

Thanks in advance
MR

Last edited by mohan705; 05-13-2010 at 11:27 AM..
# 17  
Old 05-13-2010
Try and adapt this new version (not tested)
Code:
awk -F'|' '
BEGIN {
   colDefaultLength = 1;
}
NR==1 {
   for ( c=1; c<=NF; c++ ) {
      colName[c] = $c;
   }
   colCount = NF;
   if (!colName[colCount]) colCount--;
   next;
}
{
   for ( c=1; c<=NF; c++ ) {
      if (length($c) > colLength[c]) colLength[c] = length($c);
           if ( $c ~ /^ *$/ )                     continue;
           if ( $c ~ /^[0-9]+$/        )          colIsNum[c]++
      else if ( $c ~ /^[0-9]*\.[0-9]*$/ )         colIsDec[c]++
      else if ( $c ~ /^[0-9]+\/[0-9]+\/[0-9]+$/ ) colIsDat[c]++
      else                                        colIsTxt[c]++;
   }
}
END {
   print "create table tablename("
   for ( c=1; c<=colCount; c++ ) {

      if ( colLength[c]+0 == 0 ) colLength[c] = colDefaultLength;
      var2Len = colLength[c];
           if (var2Len <   50) var2Len = 50
      else if (var2Len <  100) var2Len = 100
      else if (var2Len <  500) var2Len = 500
      else if (var2Len < 1000) var2Len = 1000;

      out = "  " colName[c];
           if ( colIsTxt[c] ) out = out " varchar2(" var2Len ")"
      else if ( colIsDat[c] ) out = out " date"
      else if ( colIsDec[c] ) out = out " number(38,4)"
      else if ( colIsNum[c] ) out = out " number(" colLength[c] ")"
      else                    out = out " varchar2(" var2Len ")";
      if ( c != colCount ) out = out ",";

      print out

   }
   print ");";
}
' inputfile

Jean-Pierre.
# 18  
Old 05-17-2010
Hi Aigles,

Thanks a lot,its working fine.

Regards,
MR
# 19  
Old 05-20-2010
Hi Aigles,

The script workinf fine if last filed is null and data also null(After col4) ,if any column data also null,its giving Error data type .For Instance for 2 column having null values ,when executing the script its giving Error data type.Is there any way to check only last field column name ,if column name exist and then no data set string 5o or if no column name and no data,consider upto col4.Your help is greatly appreciated.

Code:
col1|col2|col3|col4|
xxx| |10/08/2003|234506|
yyy| |12/03/2003|345678|

output
Code:
 
create table (col1 varchar2(50),
                  col2 Error!,
                  col3 date,
                  col4 number)

Regards,
MR
# 20  
Old 05-20-2010
Quote:
Originally Posted by mohan705
Hi Aigles,

The script workinf fine if last filed is null and data also null(After col4) ,if any column data also null,its giving Error data type .For Instance for 2 column having null values ,when executing the script its giving Error data type.Is there any way to check only last field column name ,if column name exist and then no data set string 5o or if no column name and no data,consider upto col4.Your help is greatly appreciated.

Code:
col1|col2|col3|col4|
xxx| |10/08/2003|234506|
yyy| |12/03/2003|345678|

output
Code:
 
create table (col1 varchar2(50),
                  col2 Error!,
                  col3 date,
                  col4 number)

Regards,
MR
Are you sure of your script ?
The string 'Error!' doesn't exist in the script.
When I execute the script of my last post with your last sample datas, the result is :
Code:
create table tablename(
  col1 varchar2(50),
  col2 varchar2(50),
  col3 date,
  col4 number(6)
);

A new version !
- Column name and no data -> Varchar2(50)
- No column name and datas -> column name set by the scriipt = Column<#column>
- No column name and no data -> column ignored
Code:
awk -F'|' '
BEGIN {
   colDefaultLength = 1;
}
NR==1 {
   for ( c=1; c<=NF; c++ ) {
      colName[c] = $c;
   }
   colCount = NF;
   if (!colName[colCount]) colCount--;
   next;
}
{
   for ( c=1; c<=NF; c++ ) {
      if (length($c) > colLength[c]) colLength[c] = length($c);
           if ( $c ~ /^ *$/ )                     continue;
           if ( $c ~ /^[0-9]+$/        )          colIsNum[c]++
      else if ( $c ~ /^[0-9]*\.[0-9]*$/ )         colIsDec[c]++
      else if ( $c ~ /^[0-9]+\/[0-9]+\/[0-9]+$/ ) colIsDat[c]++
      else                                        colIsTxt[c]++;
   }
}
END {
   print "create table tablename("
   for ( c=1; c<=colCount; c++ ) {

      if ( colName[c] == "" ) {
         if (colLength[c]+0 == 0) {
            continue;
         } else {
            colName[c] = "Column" c;
         }
      }


      if ( colLength[c]+0 == 0 ) colLength[c] = colDefaultLength;
      var2Len = colLength[c];
           if (var2Len <   50) var2Len = 50
      else if (var2Len <  100) var2Len = 100
      else if (var2Len <  500) var2Len = 500
      else if (var2Len < 1000) var2Len = 1000;

      out = "  " colName[c];
           if ( colIsTxt[c] ) out = out " varchar2(" var2Len ")"
      else if ( colIsDat[c] ) out = out " date"
      else if ( colIsDec[c] ) out = out " number(38,4)"
      else if ( colIsNum[c] ) out = out " number(" colLength[c] ")"
      else                    out = out " varchar2(" var2Len ")";
      if ( c != colCount ) out = out ",";

      print out

   }
   print ");";
}
' inputfile

Inputfile
Code:
MyCol1|MyCol2|||MyCol5|MyCol6|
xxx| |111||10/08/2003|234506|
yyy| |222||12/03/2003|345678|

Result:
Code:
create table tablename(
  MyCol1 varchar2(50),
  MyCol2 varchar2(50),
  Column3 number(3),
  MyCol5 date,
  MyCol6 number(6)
);

Jean-Pierre.
# 21  
Old 06-21-2010
Hi Aigles,

I need some help on this script .In the script passing 2 paramtere ,one is file name and table name but the last column not printing (highlighted with bold color)

I/P file
Code:
 cat a2.csv
ID|ID2|ID3|ID4|ID5|ID6
7666303|7666|73037341|XXX|XXXX-YYYY/Z|

Script
Code:
file=$1
table=$2
awk -F'|' '
BEGIN {
   colDefaultLength = 1;
}
NR==1 {
   for ( c=1; c<=NF; c++ ) {
      colName[c] = $c;
   }
   colCount = NF;
   if (!colName[colCount]) colCount--;
   next;
}
{
   for ( c=1; c<=NF; c++ ) {
      if (length($c) > colLength[c]) colLength[c] = length($c);
           if ( $c ~ /^ *$/ )                     continue;
           if ( $c ~ /^[0-9]+$/        )          colIsNum[c]++
      else if ( $c ~ /^[0-9]*\.[0-9]*$/ )         colIsDec[c]++
      else if ( $c ~ /^[0-9]+\/[0-9]+\/[0-9]+$/ ) colIsDat[c]++
      else                                        colIsTxt[c]++;
   }
}
END {
   print "create table tablename("
   for ( c=1; c<=colCount; c++ ) {

      if ( colName[c] == "" ) {
         if (colLength[c]+0 == 0) {
            continue;
         } else {
            colName[c] = "Column" c;
         }
      }


      if ( colLength[c]+0 == 0 ) colLength[c] = colDefaultLength;
      var2Len = colLength[c];
           if (var2Len <   50) var2Len = 50
      else if (var2Len <  100) var2Len = 100
      else if (var2Len <  500) var2Len = 500
      else if (var2Len < 1000) var2Len = 1000;

      out = "  " colName[c];
           if ( colIsTxt[c] ) out = out " varchar2(" var2Len ")"
      else if ( colIsDat[c] ) out = out " date"
      else if ( colIsDec[c] ) out = out " number(38,4)"
      else if ( colIsNum[c] ) out = out " number(" colLength[c] ")"
      else                    out = out " varchar2(" var2Len ")";
      if ( c != colCount ) out = out ",";

      print out

   }
   print ");";
}
' $file

./a.sh a2.csv tablename
create table tablename(
  ID number(7),
  ID2 number(7),
  ID3 number(8),
  ID4 varchar2(50),
  ID5 varchar2(50),
 varchar2(50)
);

Thanks in advance
MR
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Create table within awk-if statement

Hi I am trying to create a table within an awk if statement awk -F, '{ if ($8 ~ /Match/) BEGIN{print "<table>"} {print "<tr>";for(i=1;i<=NF;i++)print "<td>" $i"</td>";print "</tr>"} END{print "</table>"}' SN1.csv | mailx -s "Your details" abc@123.com But this doesnt work.. Please suggest (8 Replies)
Discussion started by: sidnow
8 Replies

2. Shell Programming and Scripting

Create table using tbl command

Hi, I required the output like attached: I tried using the below command and i'm getting error: file with data: .TS box; cB s s c|c|c PO Download statistics - Host to SOM Time;Receive Time;Processing Time;PO count 4.30 AM OMHPO File;Tue Feb 21 04:39:55 EST 2012;Tue Feb... (3 Replies)
Discussion started by: cns1710
3 Replies

3. Shell Programming and Scripting

Create Script from table

I have a table ABC with 4 columns and below data Col1,Col2,Col3,Col4 prod,10,12,joba prod,10,11,jobb qa,10,12,jobc I want to create an output file like this Server:prod StartTime:10 EndTime:12 JobName:joba Server:prod StartTime:10 EndTime:11 JobName:jobb (3 Replies)
Discussion started by: traininfa
3 Replies

4. Shell Programming and Scripting

Create Pivot table

I would like to use awk to parse a file with three columns in, like: Chennai,01,1 Chennai,07,1 Chennai,08,3 Chennai,09,6 Chennai,10,12 Chennai,11,19 Chennai,12,10 Chennai,13,12 Kerala,09,2 AP,10,1 AP,11,1 Delhi,13,1 Kerala,13,3 Chennai,00,3 Chennai,01,1 Chennai,02,1 Chennai,07,5 (3 Replies)
Discussion started by: boston_nilesh
3 Replies

5. UNIX and Linux Applications

create table via stored procedure (passing the table name to it)

hi there, I am trying to create a stored procedure that i can pass the table name to and it will create a table with that name. but for some reason it creates with what i have defined as the variable name . In the case of the example below it creates a table called 'tname' for example ... (6 Replies)
Discussion started by: rethink
6 Replies

6. UNIX for Dummies Questions & Answers

Create a table - very new to unix

I need to create a simple table of information by grepping several columns from various files and display them all at once with simple headers on top. Can anyone help get me started? I am very new to unix so I really have no idea how to work with this and I appreciate any help I can get! Let me... (11 Replies)
Discussion started by: aj250
11 Replies

7. Shell Programming and Scripting

to create an output file as a table

Hi, I have four input files and would like to create an output file as a table. Please check the example below. File 1. 111111 222222 333333 444444 File 2. 555555 666666 777777 888888 File 3. aaaaa bbbbb ccccc ddddd (2 Replies)
Discussion started by: marcelus
2 Replies

8. Shell Programming and Scripting

[ORACLE] CREATE TABLE in bash

Hey, I want to create a table in bash, my db server is oracle. You can find my script below: I am very confused. I tried to run this script many times without any luck. Please help! (6 Replies)
Discussion started by: radek
6 Replies

9. BSD

How to create IP table at Free BSD

Now, I had installed free bsd at my office. Unfortunitely, Email server have been using Local PoP3 and SMTP to our ISP with outlook. but my unix firewall sever ( free bsd ) didn't allow these port ( 110 & 25 ). How can i create the IP table to pass at server. If u have any experience about obvious... (4 Replies)
Discussion started by: Ashraff Ali
4 Replies

10. Shell Programming and Scripting

create new table/field

Dear Folks, If I have 2 files, say A and B in format: A: $1 $2 01032 12856 01041 13351 01042 11071 01042 12854 01042 12862 01042 12866 . . . and B: (2 Replies)
Discussion started by: Gr4wk
2 Replies
Login or Register to Ask a Question