Unix Shell - Multiple lines being replaced by one

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > May 2004 > Multiple lines being replaced by one





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Multiple lines being replaced by one
frank

2004-05-11, 5:40 pm

I am looking to replace a multi-line function call with another function
call that only needs one of the original variables (specifically the 2nd
argument):
function_to_be_replaced(arg1, arg2, arg3, arg4
arg5, arg6,
arg7);

I want to replace it with:
new_func(arg2);

I have started on an SED solution, but it only changes the first line,
leaving any other lines alone. That means I have to go in to do cleanup in
a lot of programs. It seemed the way to go, since using the "s" command, I
can specify tokens.

/^[& #91;:blank:]]*function_to_be_replaced(ar
g1,.*);/blabel1
/^[& #91;:blank:]]*function_to_be_replaced(ar
g1,.*/, /);/ {
:label1

s/function_to_be_replaced(arg1,[[:blank:]]*\([_a-zA-Z0-9]*\),.*/new_func(\1)
;/
}

Any ideas?


Ed Morton

2004-05-11, 9:11 pm



frank wrote:
> I am looking to replace a multi-line function call with another function
> call that only needs one of the original variables (specifically the 2nd
> argument):
> function_to_be_replaced(arg1, arg2, arg3, arg4
> arg5, arg6,
> arg7);
>
> I want to replace it with:
> new_func(arg2);
>
> I have started on an SED solution, but it only changes the first line,
> leaving any other lines alone. That means I have to go in to do cleanup in
> a lot of programs. It seemed the way to go, since using the "s" command, I
> can specify tokens.
>
> /^[& #91;:blank:]]*function_to_be_replaced(ar
g1,.*);/blabel1
> /^[& #91;:blank:]]*function_to_be_replaced(ar
g1,.*/, /);/ {
> :label1
>
> s/function_to_be_replaced(arg1,[[:blank:]]*\([_a-zA-Z0-9]*\),.*/new_func(\1)
> ;/
> }
>
> Any ideas?


Try this:

sed -e '/function_to_be_replaced(arg1,\([_a-zA-Z0-9]*\)/,/);/c\
new_func(\1)'

Add [[:blank:]]* as appropriate.

Regards,

Ed.



>


rakesh sharma

2004-05-12, 12:34 am

"frank" <fmankal@sehcomp.ca> wrote in message news:

>
> I am looking to replace a multi-line function call with another function
> call that only needs one of the original variables (specifically the 2nd
> argument):
> function_to_be_replaced(arg1, arg2, arg3, arg4
> arg5, arg6,
> arg7);
>
> I want to replace it with:
> new_func(arg2);
>
> I have started on an SED solution, but it only changes the first line,
> leaving any other lines alone. That means I have to go in to do cleanup in
> a lot of programs. It seemed the way to go, since using the "s" command, I
> can specify tokens.
>
> /^[& #91;:blank:]]*function_to_be_replaced(ar
g1,.*);/blabel1
> /^[& #91;:blank:]]*function_to_be_replaced(ar
g1,.*/, /);/ {
> :label1
>
> s/function_to_be_replaced(arg1,[[:blank:]]*\([_a-zA-Z0-9]*\),.*/new_func(\1)
> ;/
> }
>


Since it is a multiline function call, then we can safely use the following:

sed -e '
/^[[:blank:]]*function_to_be_replaced(/,/);/!b
H;/);/!d
g;x;s/.*//;x
s/\n//g
s/,/\
/2;s/\n.*/);/
s/^\([& #91;:blank:]]*\)function_to_be_replaced(
.*,/\1new_func(/
' yourfile

Assuming that the comma is not being used within an argument.
IOW, the argument separator is not in the argument itself.
frank

2004-05-20, 5:37 pm

> Since it is a multiline function call, then we can safely use the
following:
>
> sed -e '
> /^[[:blank:]]*function_to_be_replaced(/,/);/!b
> H;/);/!d
> g;x;s/.*//;x
> s/\n//g
> s/,/\
> /2;s/\n.*/);/
> s/^\([& #91;:blank:]]*\)function_to_be_replaced(
.*,/\1new_func(/
> ' yourfile


I have put this into an external file and am currently using it to replace
the original function call with new_func (which in this case is only using
the 2nd parameter). I want to extend this now to another function which is
only going to use the original function call's 2nd, 5th and 8th parameters.
I'm having problems because I don't understand the following line:

s/,/\
/2;s/\n.*/);/

Every time I try changing it, it ends up blowing up. I thought the "\" on
the end of the first line was a continuation character and tried removing it
to no avail. Can someone explain how that portion works so that I can
understand the changes I need to make?

Thanks.


rakesh sharma

2004-05-22, 10:28 pm

"frank" <fmankal@sehcomp.ca> wrote in message news:

>
>
> I have put this into an external file and am currently using it to replace
> the original function call with new_func (which in this case is only using
> the 2nd parameter). I want to extend this now to another function which is
> only going to use the original function call's 2nd, 5th and 8th parameters.
> I'm having problems because I don't understand the following line:
>


sed -e '
/^[[:blank:]]*function_to_be_replaced(/,/);/!b
H;/);/!d
g;x;s/.*//;x
s/\n//g
# here whole function call in one line
# ps=^ func_2b_rep(a1,a2,a3,...,a8,a9,...);$
s/,/\n/8;s/\n.*//; # .*a1,a2,a3,a4,a5,a6,a7,a8
s/,/\n/7;s/,/\n/5;s/\n.*\n//; # .*a1,a2,a3,a4,a5,a8
s/,/\n/2;s/,/\n/4;s/\n.*//; # .*a1,a2,a5,a8
s/,/\n/;s/^\([& #91;:blank:]]*\)function_to_be_replaced(
.*\n/\1new_func(/
s/$/);/
' yourfile
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com