Search and replace

I have a script which has several occurences of statement like
command: $UCMDPATH/xyz abc

I would like to replace the entire line starting from $UCMDPATH with the ls -ltr command
eg:
Prior to change::
command: $UCMDPATH/xyz abc

After change:
command: ls -ltr

Is there a way to do this using search and replace in vi...

Regds,
Jobby

You want to do it in vi - right?
FOr all lines, go to command mode;
:,$s/\(command:\)\(.*\)$/\1 ls -rlt/g

See if this works.
Ranjith

Ranjit,

Sorry for the delay in responding to ur update as I was out of town for a week. The solution prescribed by you works fine only change that I made to the same was

%s/\(command:\)\(.*\)$/command: ls -ltr/gc

I needed command: $UCMDPATH/abc xyz to be replaced by command: ls -ltr

Thanx a ton for the solution..

Regds,
Jobby

Jobby,

Actually the '\1' replaces the first pattern we have matched using the parenthesis - \(command:\). I tried it in my vi and it worked right.

Ranjit,

Could you just break down the entire command for me if it is not too troublesome for you...

:,$s/\(command:\)\(.*\)$/\1 ls -rlt/g

Regds,
Jobby

':,$' means from the first line to the last line
You are trying to remember patterns when you use parenthesis. So,
s/\(command:\)\(.*\)$/ --> tells sed to remember 2 patterns; one is 'command:' and the other is rest of the line till the end of line. This can be referenced by \1 & \2.
Please read this link for more explanation.

Thanx for the help provided mate. It sure cleared out my doubts w.r.t Search and replace of regular expressions.

Regds,
Jobby

Hi Jobby ,
How r u ?
Can u guide me on how to insert "/usr/bin/rm -fr " command in vi Editor at the start of each rows ..

Hi

You can do this with the following command on vi editor

:1,$ s/command: \$UCMDPATH\/xyz abc/command: ls -ltr/g

To insert "/usr/bin/rm -fr " command in vi Editor at the start of each row you do
:%s/^/\/usr\/bin\/rm\ -fr/g

@ganesh_b

you can use following command for this. this will insert required characters at start of the line.

:%s/^/\/usr\/bin\/rm -fr /

Thanks Jobby , It's working ..

I request u all to pl dont using the word "command" while talking about string/text
its a lttle confusing..
if u want to insert "/usr/bin/rm -fr "

try
:%s_^_/usr/bin/rm -fr\ _g