script to convert CSV to SQL


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script to convert CSV to SQL
# 1  
Old 01-28-2009
script to convert CSV to SQL

I am trying to write a script to convert csv files into SQL format.
What I am missing is:
1. handling of the first row and create it as a insert into format
2. better handling of any other row, and create a line with the data. *The while read line needs a better work.
I thought of using awk.

I will appreciate any help.

This is what I have so far (taken from readfile2.sh script)
PHP Code:
# Make sure we get file name as command line argument
# Else read it from standard input device
if [ "$1" == "" ]; then
   
echo "No File - Exit"
   
exit 1
else
   
FILE="$1"
   
# make sure file exist and readable
   
if [ ! -f $FILE ]; then
        
echo "$FILE : does not exists"
        
exit 1
   elif 
[ ! -r $FILE ]; then
        
echo "$FILE: can not read"
        
exit 2
   fi
fi

# field seprator, default is :, you can use blank space or
# other character, if you have more than one blak space in
# input line then use awk utility and not the cut :)
FS=","

while read line
do
        
# store field 1
        
F1=$(echo $line|cut -d$FS -f1)
        
# store field 2
        
F2=$(echo $line|cut -d$FS -f6)
        
# store field
        
F3=$(echo $line|cut -d$FS -f7)
        echo 
"User \"$F1\" home directory is $F2 and login shell is $F3"
done $FILE 
# 2  
Old 01-28-2009
If you're going to use a "while read ... " construct, why not specify enough variables to cover what you need?

eg.

Code:
while read f1 f2 f3 f4 f5 f6 f7 f8
do
  insert f1 f6 f7
done < $FILE

"insert" might be a function you create to do the actual sql insert. Specify an f8 variable so that if there are more columns than 7 you just put everything after the 7th column into f8 which you don't need.

This elimnates the need to echo $line | cut ...

Also use the special shell variable "IFS" and set that to "," before going into the while loop I've described. Do a man on your shell and search for "IFS" for more info on this.
You probably want to set a variable IFS_HOLD=$IFS before changing the IFS variable, so that you can restore it later in the script, should you need the default IFS behavior later on.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need script to convert TXT file into CSV

Hi Team, i have some script which give output in TXT format , need script to convert TXT file into CSV. Output.TXT 413. U-UU-LVDT-NOD-6002 macro_outcome_dist-8.0.0(v1_0_2) KK:1.2.494 (1234:333:aaa:2333:3:2:333:a) 414. U-UU-LVDT-NOD-6004 ... (10 Replies)
Discussion started by: Ganesh Mankar
10 Replies

2. Shell Programming and Scripting

Convert sql file to csv file

How to convert name.sql file into name.csv file. Basically my input source file is .sql file, I need to verify data after the import of that exported sql file. Thanks for any help. (3 Replies)
Discussion started by: wamqemail2
3 Replies

3. Shell Programming and Scripting

Convert the SQL Query in Shell Script

Hi All, I have a query with output below select 'create synonym "'||TABLE_NAME||'" for '||Table_owner||'."'||table_name||'"'||chr(59) from user_synonyms; ================== create synonym "RV_SBC_SIG" for WFCONTROLLER_TE."RV_SBC_SIG"; create synonym "AQM_TASK" for AWQM_TE."AQM_TASK";... (2 Replies)
Discussion started by: pvmanikandan
2 Replies

4. UNIX for Dummies Questions & Answers

How to convert R$Timestamp in Sql*Plus within a UNIX Shell Script?

I need to compare a R$Timestamp field sql within a Unix Shell Script. In straight SQL the following code works fine: Table Name: LL_UNIT_TRANSACTION UT Field: R$Timestamp Where TRUNC(UT.R$Timestamp) >= TRUNC(SYSDATE -7) the following returns no data within the Unix Shell Script... (2 Replies)
Discussion started by: Dapconsult
2 Replies

5. Shell Programming and Scripting

Script to convert CSV file to HTML

Hi, I have made a a script which creates a csv file as daily database report However i want to covert that csv file to html because csv file does not have a good visibilty. So it is possible to have such csv to html coversion script. Your prompt help much appreciated. Thanks in advance (4 Replies)
Discussion started by: sv0081493
4 Replies

6. Shell Programming and Scripting

Convert sql output to csv file via bash tools

hi Can anybody help me with converting such structure into csv file for windows : BAT_ID ID_num CVS_LINE A_SEG SKILL_TO A_CUSTOMER_TYPE --------- ---------- --------------------------------- ---------- ------------------ ----------- 14-MAY-11 777752 ... (4 Replies)
Discussion started by: kvok
4 Replies

7. Shell Programming and Scripting

SQL Script's output to a CSV file

I need to call and execute an SQL script within a KSH script and get the output/extracted data into a CSV file. Is there any way to get the out put in a CSV file other than spooling ? I tried spooling. Problem is if there is any wrning/comment/Error they all will be spooled into the csv file. I... (4 Replies)
Discussion started by: Sriranga
4 Replies

8. Shell Programming and Scripting

Awk script to convert csv to html

Hi Written some script to convert csv to html but could not add table headers.Below are the errors iam getting ./csv2html | more + awk -v border=1 -v width=10 -v bgcolor=black -v fgcolor=white BEGIN { printf("<table border=\"%d\" bordercolor=\"%s\" width=\"%d\"... (2 Replies)
Discussion started by: zeebala1981
2 Replies

9. Shell Programming and Scripting

Is there any script which convert binary file to CSV format

Dear guys; I have a binary file and I need to convert its data to csv format ...appreciating your help. Best Regards (14 Replies)
Discussion started by: ahmad.diab
14 Replies

10. Shell Programming and Scripting

convert this into csv using awk/shell script

Hi Scripting gurus, I need to convert following text snippet into csv. please help Input heading1 = data1 heading2 = data2 .. .. heading n = data n heading 1 = data1 .. .. Output data1,data2,....,data n (3 Replies)
Discussion started by: azs0309
3 Replies
Login or Register to Ask a Question