block of name value pair to db insert statements


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting block of name value pair to db insert statements
# 1  
Old 05-10-2009
block of name value pair to db insert statements

Hi,

I need to convert the following file into DB insert statements.


Code:
$ cat input.txt
START
name=john
id=123
date=12/1/09
END

START
name=sam
id=4234
status=resigned
date=12/1/08 
END

The desired output is

Code:
$ cat output.txt
INSERT INTO tbl (name, id, date) VALUES ("john", "123", "12/1/09");
INSERT INTO tbl (name, id, status, date) VALUES ("sam", "4234", "resigned", "12/1/08");

awk, perl, sed anything is fine.

Thank you,
Vimala
# 2  
Old 05-10-2009
One way with awk:

Code:
awk -F= 'BEGIN{b1="INSERT INTO tbl ("; b2=") VALUES ("; b3=");"}
/START/{next}
/END/{print s1 s2 b3;s1=s2=""}
/name/{s1=b1 $1;s2=b2 "\"" $2"\"";next}
{s1=s1", "$1;s2=s2 ", \""$2 "\""}
'  input.txt

# 3  
Old 05-11-2009
Thank you very much Franklin52. It works well.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk name pair values

Team, I have a file like below FILE: NAM1,KEY1,VAL1 NAM1,KEY2,VAL2 NAM1,KEY3,VAL3 NAM2,KEY1,VALA NAM2,KEY2,VALB NAM2,KEY3,VALCOutput: I have to build commands like below <Script> VAL1 VAL2 VAL3 NAME1 <Script> VALA VALB VALC NAME2Can you please help with awk command i can use... (4 Replies)
Discussion started by: mallak
4 Replies

2. Shell Programming and Scripting

Parsing a name Value pair file

Hi, I have a file having rows as Row1 : model=.1.3.6.1.4.1.9.1.1047,location=abc, pollgrp=PG_CISCO4, ifindex=3, ip=10.10.10.1,parttype=Interface, devtype=Router,part=GigabitEthernet0/1,ifmtu=1520 Row2 :... (2 Replies)
Discussion started by: mksuneel
2 Replies

3. Shell Programming and Scripting

Insert id from same block

Hello all, Please help modify my file. I want to pick up the ID value when the 3rd column is either gene or protein_coding_gene and place the ID in the gene_id attribute corresponding rows which has the the keyword exon in the third column. Input file 10 ensembl gene 195359 ... (6 Replies)
Discussion started by: gina.lizar
6 Replies

4. Shell Programming and Scripting

Parse through ~21,000 Database DDL statements -- Fastest way to perform search, replace and insert

Hello All: We are looking to search through 2000 files with around 21,000 statements where we have to search, replace and insert a pattern based on the following: 1) Parse through the file and check for CREATE MULTISET TABLE or CREATE SET TABLE statements.....and they always end with ON... (5 Replies)
Discussion started by: madhunk
5 Replies

5. 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

6. Shell Programming and Scripting

Insert Block of Text into a File After a Ranged Search

Hello, I've been racking my brain trying to find a good way to accomplish a task. I need to insert a block of text into a file in the format of FirewallRuleSet proxy-users { FirewallRule allow to 0.0.0.0/0 } I need to insert this block of text (which could have sed special... (2 Replies)
Discussion started by: 0xception
2 Replies

7. UNIX for Dummies Questions & Answers

can't pair BT keyboard/mice

hello, i can't pair BT keyboard and mice under OS X leopard 10.5.7 installed on pc hardware using ipc osx86 10.5.6 final dvd. I could not find any answers on the web, nor on the osx86 forums, so i figured out maybe ill try my luck on linux/unix forum, after all from what i know os x is basicly a... (0 Replies)
Discussion started by: baron_harkonnen
0 Replies

8. Shell Programming and Scripting

how to append a block of statements after another block in the file

Hi I need to append the following block of statements in the middle of the file: # openpipe tsdbdwn2 set -x exec >> /tmp/tsdbdwn2.fifo 2>&1 # This needs to be appended right after another block of statements: if test $# -eq 0 ;then echo "Safety check - do you really wish to run" $0 "... (5 Replies)
Discussion started by: aoussenko
5 Replies

9. Shell Programming and Scripting

Complex Insert block in the Script

I have script in that there are thousands of create statement... I want to add these lines, above every Create Stament ================================================ IF OBJECT_ID('dbo.account_account_relations') IS NOT NULL BEGIN DROP TABLE dbo.account_account_relations IF... (2 Replies)
Discussion started by: niceboykunal123
2 Replies
Login or Register to Ask a Question