1748169 Members
4304 Online
108758 Solutions
New Discussion юеВ

Re: sed problem

 
SOLVED
Go to solution
Declan Heerey
Frequent Advisor

sed problem

I have a small sed problem. Basically i am trying to do the following 3 things

1) Search a file for a block of text

2) Search a file for the first occurance of a string

3) Insert a block of text

I've tried various combonations of sed commands with little luck so any help appreciated

Thanks in advance

Declan
9 REPLIES 9
Alexander Chuzhoy
Honored Contributor

Re: sed problem

Try the following

sed s/string_you_search_for/new_string\ string_you_search_for/ filename


This should print on STDOUT the result. If you wish to modify the content of the file as well, then simply redirect the output to some file and then rename the new file to the name of the original file ( after you did a backup of the original file of course).

Declan Heerey
Frequent Advisor

Re: sed problem

Alex,

I only want to replace the first occurance of the word and i need to replace it with a block of text.

my file is like this

Declare

Begin

EXECUTE IMMEDIATE 'BEGIN WriteScriptsLogs( :2, :3, :4, :5, :6, :7, :8, :9 ); END;'
USING IN s_Source, IN s_ScriptName, IN s_ScriptSetVer, IN s_SetRange, IN n_ScriptVer
, IN s_Status, IN s_Message, OUT s_Return;

End;

I need to search for the EXECUTE IMMEDIATE block until the s_Return and replace it with another different block
James R. Ferguson
Acclaimed Contributor

Re: sed problem

Hi Declan:

If you don't mind Perl:

# perl -0377 -pe 's/EXECUTE IMMEDIATE.+s_Return/NEW\nBLOCK\nHERE/s' file

If you would like to update the file "in-place", retraining an un-modified backup copy as "file.old", do:

# perl -0377 -p.old -e 's/EXECUTE IMMEDIATE.+s_Return/NEW\nBLOCK\nHERE/s' file

Regards!

...JRF...
Declan Heerey
Frequent Advisor

Re: sed problem

Thanks James,

I don't mind using perl but i had to change the command slightly by removing the .old after the -p otherwise i got the following error

Unrecognized switch: -.old (-h will show valid options).

now i output the result to a second file

any idea how to search for the first occurance of a word in the same file i.e. if i have multiple blocks which are the same?
James R. Ferguson
Acclaimed Contributor

Re: sed problem

Hi (again) Declan:

Sorry, I droppted a ".":

# perl -pi.old -e ...

Anyway, for your first-only block:

# perl -0377 -pi.old -e 's/EXECUTE IMMEDIATE.+s_Return;(.+Declare.+)/NEW\nBLOCK\nHERE\1/s' file

Regards!

...JRF...

James R. Ferguson
Acclaimed Contributor

Re: sed problem

Hi (again) Declan:

Actually, I prefer:

# perl -0377 -pi.old -e 's/(EXECUTE IMMEDIATE.+s_Return;)(?=.+EXECUTE IMMEDIATE.+s_Return;)/NEW\nBLOCK\nHERE/s' file

Regards!

...JRF...
Declan Heerey
Frequent Advisor

Re: sed problem

Thanks James,

I'll give this a whirl asap and assign point.

Thanks again
James R. Ferguson
Acclaimed Contributor
Solution

Re: sed problem

Hi Declan:

Disregard yesterday's bungles, please. I was not thinking clearly at all! The following should work for one or n-blocks of text as you have shown:

# perl -0377 -pi.old -e 's/(EXECUTE IMMEDIATE.+?s_Return;)/NEW\nBLOCK\nHERE/s' file

Regards!

...JRF...
Declan Heerey
Frequent Advisor

Re: sed problem

Nice one James that works a treat (only thing was i had to double quote the string)

Thanks again