Appending string, variable to file at the start and string at end


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Appending string, variable to file at the start and string at end
# 1  
Old 03-24-2010
Appending string, variable to file at the start and string at end

Hi ,

I have below file with 13 columns. I need 2-13 columns seperated by comma and I want to append each row with a string "INSERT INTO xxx" in the begining as 1st column and then a variable "$node" and then $2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13 and at the end another string " ; COMMIT;"

It is not working . Can u please make this one work or new one .....


Code:
=============
V1S="INSERT INTO schema.table VALUES (" <-- mostly constant value string
V1E=") ; commit ;" <-- mostly constant value string

node=$1 <-- passing value and varies (lets say here it is 10)

awk '{print $V1S $node ","$2","$3","$4","$5","$6","$7","$8","$9","$10","$11","$12","$13 $V1E}' File1 > File2

Source_File:
-------------
0x0780000072F98E00 17 155 n/a 17 155 TAB1 SCH1 Perm 1 0 1 0
0x07800000747BF380 36 475 n/a 36 475 TAB1 SCH1 Perm 2 0 0 0
0x078000009A2A8600 36 476 n/a 36 476 TAB1 SCH1 Perm 11396 0 0 0

Expected Result in Target File:
-----------------------------
INSERT INTO schema.table VALUES (10,17,155,n/a,17,155,TAB1,SCH1,Perm,1,0,1,0); commit;
INSERT INTO schema.table VALUES (10,36,475,n/a,36,475,TAB2,SCH1,Perm,2,0,0,0); commit;
INSERT INTO schema.table VALUES (10,36,476,n/a,36,476,TAB2,SCH1,Perm,11396,0,0,0); commit;

Appreciate your help ...

Shyam

---------- Post updated at 11:24 AM ---------- Previous update was at 11:09 AM ----------

awk '{print "INSERT INTO scehma.table VALUES (" $2 "," $3 "," $4 "," $5 "," $6 "," $7 "," $8 "," $9 "," $10 "," $11 "," $12 "," $13 ") ; commit ;"}' File1 > File2

is working but how to get the "$node" varying variable value ???
# 2  
Old 03-24-2010
you need to pass the variable in using something like:

Code:
awk -v node=$1 '{...

HTH
# 3  
Old 03-24-2010
Code:
node=10
sed "s/\(.[^ \t]*\) \(.*\)/INSERT INTO schema.table VALUES($node \2/;s/ /,/4g;s/$/);commit;/" file

# 4  
Old 03-24-2010
Thanks for the solution .. i am close but few more thingsg..

it is putting comma for every space in the result " see INSERT,INTO,schema.table,VALUES,(" and " 0,);commit ;"

Code
-------
V1S="INSERT INTO schema.table VALUES ("
V1E=");commit ; "
node=$1 ( here it is 10)
sed "s/\(.[^ \t]*\) \(.*\)/$V1S$node \2/;s/ /,/4g;s/$/$V1E/" File1 > File2
cat File2 | head -3
echo '' "


Result
--------
INSERT,INTO,schema.table,VALUES,(10,17,155,n/a,17,155,TAB1,SCH1,Perm,1,0,1,0,);commit ;
INSERT,INTO,schema.table,VALUES,(10,36,475,n/a,36,475,TAB2,SCH1,Perm,2,0,0,0,);commit ;
INSERT,INTO,schema.table,VALUES,(10,36,476,n/a,36,476,TAB3,SCH1,Perm,11396,0,0,0,);commit ;

One more thing , I also need ' " ' (double quotes) around 4th, 7th, 8th & 9th columns

We are close.. Something small is missing..
Shyam

Last edited by Vaddadi; 03-24-2010 at 02:34 PM..
# 5  
Old 03-24-2010
Quote:
Originally Posted by Vaddadi
We are close.. Something small is missing.. Shyam
its GNU sed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do delete certain lines alone which are matching with start and end string values in file?

Hi, In my previous post ( How to print lines from a files with specific start and end patterns and pick only the last lines? ), i have got a help to get the last select statement from a file, now i need to remove/exclude the output from main file: Input File format: SELECT ABCD, DEFGH,... (2 Replies)
Discussion started by: nani2019
2 Replies

2. Shell Programming and Scripting

Search a String between start and end of a block in a file

Hi, I have a scenario where I want to display the output based on the pattern search between the start and end of a block in a file, we can have multiple start and end blocks in a file. Example give below, we need to search between the start block abc and end block def in a file, after that... (5 Replies)
Discussion started by: G.K.K
5 Replies

3. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

4. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

5. Shell Programming and Scripting

Check file for string existence before appending it with string

I want to append file with a string but before doing that i want to check if this string already exist in that file.I tried with grep on Solaris 10 but unsuccessful.Man pages from grep seems to suggest if the string is found command status will be 0 and if not 1.But i am not finding it.May be i... (2 Replies)
Discussion started by: sahil_shine
2 Replies

6. Shell Programming and Scripting

Remove lines between the start string and end string including start and end string Python

Hi, I am trying to remove lines once a string is found till another string is found including the start string and end string. I want to basically grab all the lines starting with color (closing bracket). PS: The line after the closing bracket for color could be anything (currently 'more').... (1 Reply)
Discussion started by: Dabheeruz
1 Replies

7. UNIX for Dummies Questions & Answers

Appending a character(#) with string search at the start of the line

Hello, I have been browsing through the forum, but unable to find a solution for my requirement. I need to go through a file and search for /home/users and insert a # symbol at the start /home/users. Example output is #/home/users. Can you please help me with the awk or sed command for... (1 Reply)
Discussion started by: chandu123
1 Replies

8. Shell Programming and Scripting

Appending string at the end of the file

Hello, I wanted to append 'XYZ' at the end of the text file. How can i do this? I searched the forums and i am not getting what i want. Any help is highly appreciated. Thanks (2 Replies)
Discussion started by: govindts
2 Replies

9. Shell Programming and Scripting

String as both start and end anchors in awk

Not sure if the title of this thread makes sense, but hopefully my explanation will. I'm using awk to print some stats from an apache accesslog. I would like to specify the regexp condition where only the two root pages of "index.html" and "/" are counted in my results. What I can't figure out... (3 Replies)
Discussion started by: picassolsus
3 Replies

10. Shell Programming and Scripting

appending string to text file based on search string

Hi, I need to append string "Hi" to the beginning of the lines containing some specific string. How can I achieve that? Please help. Malay (1 Reply)
Discussion started by: malaymaru
1 Replies
Login or Register to Ask a Question