The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
insert multiple lines into a file c0mrade Shell Programming and Scripting 12 09-04-2008 04:04 PM
parsing error in if statement rakeshou Shell Programming and Scripting 2 09-25-2007 10:46 AM
How to insert tab at specified column.HELP sslr UNIX for Dummies Questions & Answers 7 09-21-2007 10:54 AM
Insert TAB in echo statement sunils27 Shell Programming and Scripting 5 08-26-2005 03:36 AM
awk command for INSERT statement nattynatty Shell Programming and Scripting 4 05-10-2002 02:11 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-08-2007
jjordan jjordan is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 1
(sed) parsing insert statement column that crosses multiple lines

I have a file with a set of insert statements some of which have a single column value that crosses multiple lines causing the statement to fail in sql*plue. Can someone help me with a sed script to replace the new lines with chr(10)?

here is an example:
insert into mytable(id, field1, field2)
values ('1', 'myvarchar', 'my varchar
that has carriage returns, and is making
my script die');

insert into mytable(id, field1, field2)
values ('2', 'myvarchar2', 'my varchar
that has carriage returns, and is making
my script die. This one is even trickier because
it contains a quote that I didn't expect.
');

Note that the value for field2 (third column) crosses multiple lines. The first case I would need to replace each new line with:
' || chr(10)
and the start of the following line with:
||'

resulting in:
insert into mytable(id, field1, field2)
values ('1', 'myvarchar', 'my varchar '||chr(10)||'that has carriage '|| chr(10) ||'returns, and is making '|| chr(10) ||' my script die');

In the second case I would need to do the same as the first case with the following addtions.

1. I need to handle the single quote by using '''
2. because the last line doesn't have any text I can replace the last carriage return with
'||chr(10)

resulting in:
insert into mytable(id, field1, field2)
values ('2', 'myvarchar2', 'my varchar '||chr(10)|| 'that has carriage returns, and is making '||chr(10)||'my script die. This one is even trickier because '||chr(10)||'it contains a quote that I didn't expect.');

This has really stumped me and I am hoping there are some guru's out there that can help me out. So far I have come up with this script that prints the lines that I want to process:

/,.*'[^';]*$/p

The way I read it is search for a "," followed by 0 or more characters (I wanted just whitespace but \s doesn't work) followed by a single quote and 0 or more characters but not ' ; before the end of the line. Put simply, a single quote optional characters but not the end quote nor end statement before the end of the line.

So I expect that this finds the start of patterns like in the case #1 now I need to find the end of the pattern and process the chunk like I mentioned above.

Anyway, I'd really appreciate some help. T.I.A.

JJ
  #2 (permalink)  
Old 10-08-2007
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,078
awk and how to use <'> in awk

Hi,
Hope can help me. Actually, i do not know how to use single quote<'> in awk. So i use <"> insteand of <'> and the output is ok.
If anyone know how to use single quote<'> in awk, pls kindly inform me. Thanks in advanced.
Output:
Code:
insert into mytable(id, field1, field2)
values ('1', 'myvarchar', 'my varchar"||chr(10)
||"that has carriage returns, and is making "||chr(10)
||"my script die');

insert into mytable(id, field1, field2)
values ('2', 'myvarchar2', 'my varchar"||chr(10)
||"that has carriage returns, and is making "||chr(10)
||"my script die. This one is even trickier because "||chr(10)
||"it contains a quote that I didn't expect."||chr(10)
||"');
code:

Code:
awk '
{
if (index($0,"values")!=0)
	n=1;
if (index($0,");")!=0)
	n=3
if(n==1)
	{
		print $0"\"||chr(10)"
		n=2
	}
else if(n==2)
	print "||\""$0"\"||chr(10)"
else if (n==3)
	{
		print "||\""$0
		n=0
	}
else
	print $0
}' filename
  #3 (permalink)  
Old 10-08-2007
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Quote:
Originally Posted by summer_cherry View Post
Hi,
Hope can help me. Actually, i do not know how to use single quote<'> in awk. So i use <"> insteand of <'> and the output is ok.
If anyone know how to use single quote<'> in awk, pls kindly inform me. Thanks in advanced.
Code:
awk -v q="'" -v qq='"' '{print q $0 q}' myFile
  #4 (permalink)  
Old 10-09-2007
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
Or...
Code:
awk '{print "\042" $0 "\047"}' myFile
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:11 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0