LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-17-2009, 09:09 AM   #1
D-a-a-n
LQ Newbie
 
Registered: Aug 2008
Posts: 7

Rep: Reputation: 0
Red face using sed for transformation text


Hi,

I'm a sed newbie and need this:

DBMS_OUTPUT.PUT_LINE("text " ||table1|| " moretext: " ||table2||);

To be converted in something like this:

RAISE DEBUG "text % moretext: %";

(^^The two '%' signs represent table1 & table2).

So, I need to replace all values on that line surrounded by || in % signs, and all other text ( 'moretext:' ) just needs to be put in the string, in the right place.

I find it really hard to find the right syntax for this sed command, so if someone could give me some hints I would be really happy

Greetz Daan

ps: please consider that there could be more than 2 extra fields, and more textfields.
 
Old 04-17-2009, 02:48 PM   #2
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 88
I'm not sure that sed is flexible enough for what you want. I'm fairly certain that awk would be, but I only use it for fairly simple things, so I wouldn't be able to help there. My weapon of choice for this sort of thing would be Perl.

Assuming the simple structure you show here, where the string is effectively delimited by double-quotes, and the text fields and tables alternate, this simple little script would work:
Code:
#!/usr/bin/perl

$str2mung = $ARGV[0];

$munged_str = "RAISE DEBUG \"";

(@mungarray) = split('"',$str2mung);

for ($i = 1; $i <= $#mungarray; $i += 2) {
   $munged_str .= "$mungarray[$i]%";
}  

print "$munged_str\";\n";
It would take the input string as a command line argument (protected by single-quotes):
Code:
rob:perl$ ./sedsubs.pl 'DBMS_OUTPUT.PUT_LINE("text " ||table1|| " moretext: " ||table2||)'
RAISE DEBUG "text % moretext: %";
rob:perl$
If you need to do something more complex, then Perl is definitely the beast for you - Beginning Perl

Rob

Last edited by Robhogg; 04-17-2009 at 03:15 PM. Reason: Added link to Beginning Perl
 
Old 04-17-2009, 02:54 PM   #3
emetib
Member
 
Registered: Feb 2003
Posts: 482

Rep: Reputation: 33
here's some reference for you.
http://www.grymoire.com/Unix/Sed.html
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
using sed to replace text on one line in a text file vo1pwf Linux - Newbie 5 06-24-2009 08:54 AM
LXer: Simple Text Transformation Tool LXer Syndicated Linux News 0 05-08-2008 07:20 AM
sed: delete text till <pattern2> depending on length of text oyarsamoh Programming 2 05-05-2007 02:40 AM
Text transformation Sabinou Linux - General 6 04-15-2007 01:01 PM
SED - display text on specific line of text file 3saul Linux - Software 3 12-29-2005 05:32 PM


All times are GMT -5. The time now is 10:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration