Operating System - HP-UX
1826007 Members
3230 Online
109690 Solutions
New Discussion

Re: vi : can i execute a substitution at shell prompt?

 
SOLVED
Go to solution
Jessica Chou
Advisor

vi : can i execute a substitution at shell prompt?

I need to substitude a certain string of files under a specific directory. for instance, in *.c, a string
#include
need to be changed to
#include

Can I kind of combine "find", "grep" and "vi"=>
:1,$s/string.h/String.h/g
to accomplish it?

Please help. Thank you.

Jessica

7 REPLIES 7
Madhu Sudhan_1
Respected Contributor
Solution

Re: vi : can i execute a substitution at shell prompt?

Jessica :
Please find attached the script which does the same thing what you wanted. I think this is the cleaner way than using vi to do the job.

Hope this helps.

PS: Use the Script as
# t.sh *.c

Enjoy !
Think Positive
curt larson
Frequent Advisor

Re: vi : can i execute a substitution at shell prompt?

you can do it this way:

re=string.h
sub_re=String.h

find your_dir -name "*.h" |
while read filename
do
ex -sc "1,$ s/$re/$sub_re/g | wq" $filename
done

nobody else has this problem
Madhu Sudhan_1
Respected Contributor

Re: vi : can i execute a substitution at shell prompt?

Jessica:
A small note to you. Use the script as

# t.sh *.c

You have to use "" to negate the what shell has about it.

......Madhu

Think Positive
Madhu Sudhan_1
Respected Contributor

Re: vi : can i execute a substitution at shell prompt?

Iam trying to put a backward slash and it is not getting printed in the post.
Think Positive
Sid Shapiro
Occasional Advisor

Re: vi : can i execute a substitution at shell prompt?

Download a command "rpl" from hpux porting center. It is very handy command.
Deepak Seth
Kevin Ernst
Regular Advisor

Re: vi : can i execute a substitution at shell prompt?

Curt's suggestion of using 'ex' is probably the fastest way of doing it with tools you already have on your system, and *without* creating any intermediate files. Just be very sure the syntax of your substitution ("1,$s/this/that") command is correct before hitting the 'Enter' key, as you would have no way of going back if you make a mistake.

Madhu's script uses 'sed,' which cannot modify the files directly, but uses an intermediate file to store the results before overwriting the original file. As an added safety net, you could modify his script so that the intermediate file is preserved (so you can verify the changes), and the original file is left untouched.
Madhu Sudhan_1
Respected Contributor

Re: vi : can i execute a substitution at shell prompt?

Thanks Kevin, Your point noted.

......Madhu
Think Positive