Visit Our UNIX and Linux User Community


Create table


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create table
# 1  
Old 05-05-2010
Create table

Hi

I want create table based on csv file .I have come up with some informations getting columns names and then no idea from that
.Please any help

Code:
file.txt

col1|col2|col3|col4|col5
1234|zxxxx|xcvvv|300|null

file.sh

file=$1
c1=`head -1 $file|tr "|" "\n" |cat -n`
echo "$c1"
cnt=`echo "$c1"|nawk -F " " '{print $1}'`
#echo "$cnt"

for i in `echo $c1`
do
echo $i
done

Thanks in advance
MR

Last edited by Yogesh Sawant; 05-05-2010 at 04:44 AM..
# 2  
Old 05-05-2010
If file.txt is your source file, what is the expected output?
# 3  
Old 05-05-2010
Expecting like that

"create table tablename(
col1 varchar2(10),
col2 number(10,2)
...).


Regards,
MR
# 4  
Old 05-05-2010
Hi,

How u will identify the datatypes with width of the coloumns.
# 5  
Old 05-05-2010
Hi
Sorry for not providing complete information
Code:
using the below ,will find out max length
 awk -F "|" '{ if (length($83) > max) max = length($83) } END { print max }'    file


cat filename|egrep '[0-9]' ---for numbers

car filename |egrep '[a-z][A-Z]' --for strings

# 6  
Old 05-05-2010
Something like that ?
Code:
awk -F'|' '
NR==1 {
   for (c=1; c<=NF; c++) {
      colName[c] = $c;
   }
   colCount = NF;
   next;
}
{
   for (c=1; c<=NF; c++) {
      if ($c !~ /^[0-9]+$/) colIsText[c]++;
      if (length($c) > colLength[c]) colLength[c] = length($c);
   }
}
END {
   print "create table tablename("
   for (c=1; c<=colCount; c++) {
      out = "  " colName[c];
      if (colIsText[c]) 
         out = out " varchar2(" colLength[c] ")";
      else
         out = out " number(" colLength[c] ",2)";
      if (c != colCount) out = out ",";
      print out
   }
   print ");";
}
' inputfile

Input file:
Code:
col1|col2|col3|col4|col5
1234|zxxxx|xcvvv|300|null
12|abcdefghij|xyz|1024|comment

Output:
Code:
create table tablename(
  col1 number(4,2),
  col2 varchar2(10),
  col3 varchar2(5),
  col4 number(4,2),
  col5 varchar2(7)
);

Three problems remaining :
- Table name (fixed to tablename in the script)
- Precision for numbers (fixed to 2 in the script)
- Columns with null value
Jean-Pierre.
# 7  
Old 05-05-2010
Hi Aigles,

Thanks a lot for your reply .can you please explain? .Is there any way to check for date format.Regarding the decimal and precesion uisng awk check for numbers with decimal ,if any decimal ,want hard code to decimal(38,4) else only number with the length.

Code:
ex:

col1|col2|col3|col4|col5
1234|zxxxx|xcvvv|300.200|10/08/2008 

output
Code:
create table tablename(
  col1 number(4),
  col2 varchar2(10),
  col3 varchar2(5),
  col4 number(38,4),
  col5 date
);

Regards,
MR

Last edited by mohan705; 05-05-2010 at 07:47 AM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #885
Difficulty: Easy
Denial of service attacks (DoS) are designed to make a machine or network resource unavailable to its intended users.
True or False?

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

Featured Tech Videos