create Insert script from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting create Insert script from a file
# 1  
Old 05-21-2009
Bug create Insert script from a file

Hi ,

I have a text file text.txt which contains values as

ULTRA,OTHERS,Mumbai,16912
ULTIMATE,OTHERS,Mumbai,16913
ULTIMATIUM,OTHERS,Mumbai,16914


I want to read the file line by line and create insert scripts like

INSERT INTO TAB ( DESC,PLACE,NUMBER ) VALUES (ULTRA,OTHERS,Mumbai,16912)

INSERT INTO TAB ( DESC,PLACE,NUMBER ) VALUES (ULTIMATE,OTHERS,Mumbai,16913)

INSERT INTO TAB ( DESC,PLACE,NUMBER ) VALUES (ULTIMATUM,OTHERS,Mumbai,16914)


Please Help

Thanks and Regards
# 2  
Old 05-21-2009
if you have Python
Code:
#!/usr/bin/env python
for line in open("file"):
    line=line.strip().split(",")
    print "insert into tab ( DESC,PLACE,NUMBER) values( '%s','%s','%s',%s)" %(line[0],line[1],line[2],line[3]  )

output
Code:
# ./test.py
insert into tab ( DESC,PLACE,NUMBER) values( 'ULTRA','OTHERS','Mumbai',16912)
insert into tab ( DESC,PLACE,NUMBER) values( 'ULTIMATE','OTHERS','Mumbai',16913)
insert into tab ( DESC,PLACE,NUMBER) values( 'ULTIMATIUM','OTHERS','Mumbai',16914)

# 3  
Old 05-21-2009
this will do
Code:
awk '{print "INSERT INTO TAB ( DESC,PLACE,NUMBER ) VALUES ("$0");"}' filename

OR
Code:
awk -F"," '{print "INSERT INTO TAB ( DESC,PLACE,NUMBER ) VALUES ('"$1"','"$2"','"$3"',"$4");"}' filename

# 4  
Old 05-22-2009
perl:

Code:
while(<DATA>){
	chomp;
	my @tmp=split(",",$_);
	printf("insert into tab ( DESC,PLACE,NUMBER) values('%s','%s','%s',%s)\n",$tmp[0],$tmp[1],$tmp[2],$tmp[3]);
}
__DATA__
ULTRA,OTHERS,Mumbai,16912
ULTIMATE,OTHERS,Mumbai,16913
ULTIMATIUM,OTHERS,Mumbai,16914

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

TCL script to insert some text on a file

Hi All , I am looking to create one TCL script to insert one text based on some regular expression match on one file as stated below Input File module (mem1 ,mem2 , bist1 , ten2 , sen1 , land2 , taane2 , ran1 , ran2 , tri2 , tri8 , fly1 , fly2 , san2 ); output ran1 , ran2 , tri2 ,... (1 Reply)
Discussion started by: kshitij
1 Replies

2. UNIX for Dummies Questions & Answers

Insert text into a file using shell script

Hi, I need to insert "Hello World" text into a file called hai.txt using shell scripting. Kindly help me. For eg: If I open the file hai.txt by giving linux command cat hai.txt, the content of the file should have the text Hello World in it. Thanks (5 Replies)
Discussion started by: karthick nath
5 Replies

3. Shell Programming and Scripting

Script to insert a function into a C source file

Hello, I think I need sed, but perhaps awk could help. I am trying to add a function to a C source file based off a struct declaration. for example: struct Rational { int numerator; int denominator; }; becomes struct Rational (4 Replies)
Discussion started by: afulldevnull
4 Replies

4. Shell Programming and Scripting

Create SQL DML insert statements from file using AWK or similar

Hi all. This is my first post on this forum. I've previously found great help in the huge knowledgebase that is here, but this time I have not been able to find a solution to my problem. I have a large text file that looks like this: typedef struct ABC_struct_nbr1_ { char attr1; /*... (0 Replies)
Discussion started by: Yagi Uda
0 Replies

5. UNIX for Dummies Questions & Answers

Shell script find word from one file and insert in another file

Hi, I am new to shell scripting. I need a bash shell scripts which search and grep a parameter value from input.txt file and insert it in between two semicolon of second line of output.txt file. For example The shell script search an IP address as parameter value from input.txt ... (2 Replies)
Discussion started by: sunilkumarsinha
2 Replies

6. Shell Programming and Scripting

need to create a insert query for a file

Hi Guys, I need to create a insert query for the below file Fri Sep 4 06:25:51 2009 ACTION : 'CREATE INDEX S100S_DC.PLInsuranceReportRules_testI1 ON S100S_DC.PLInsuranceReportRules_test1(ENTITY_KEY)' DATABASE USER: '/' PRIVILEGE : SYSDBA CLIENT USER: oracle CLIENT TERMINAL: pts/3... (6 Replies)
Discussion started by: mac4rfree
6 Replies

7. Shell Programming and Scripting

awk script to insert text in file

I'm in a time crunch here and I don't know how to write scripts. I have a file of data that looks like the following: 1 7.652073E+00 0.000000E+00 7.146691E+02 1.704154E+01 2 7.652068E+00 6.031387E+00 7.146636E+02 2.499305E+01 3 7.645906E+00 1.509455E+01 7.144158E+02 1.822061E+02 4... (7 Replies)
Discussion started by: pk218703
7 Replies

8. UNIX for Dummies Questions & Answers

Need Script to insert colons in each line of file

I have a file with over 500 MAC addresses. Each address is on a new line. However, the MACs do not have ":" I need a script that will read the file, line by line and insert colons in the addresses and then print the results to a new file. current.txt looks like this 111111111111 222222222222... (4 Replies)
Discussion started by: canopus15
4 Replies

9. UNIX for Dummies Questions & Answers

How to insert new line in the data file using the script

Hi all, I have a test.dat file.In that file i have many lines of data. IN between some lines i want to insert a new line while running the test.ksh. Say for ex: In the dat file i have data like N001 100.00 N001 200.00 N001 300.00 N001 400.00 <== After this line i want to... (2 Replies)
Discussion started by: Sona
2 Replies
Login or Register to Ask a Question