Operating System - HP-UX
1752296 Members
5049 Online
108786 Solutions
New Discussion юеВ

using ex in commandline for exchanging texts in a file

 
SOLVED
Go to solution
Klaus  Frank
Frequent Advisor

using ex in commandline for exchanging texts in a file

Hallo to all

I'am looking for a comfortable way to replace all words "old" to "new" in a textfile without entering vi or ex interactively.
Sofar I started with

# ex +%s/old/new/g ..... my_text_file

This works allright but leaves me with the ex-prompt :
i.e. I have to enter :wq! to save the changes and return to my shell.
All attempts to add somewhat like | wq! did not work. How can I use ex to do the change and return to my shell without beeing prompted by ex?

All other ideas for one-line-solutions are welcome

thanks
... we all can make it with a little help ...
3 REPLIES 3
Dan Hetzel
Honored Contributor

Re: using ex in commandline for exchanging texts in a file

Hi Klaus,

You should use 'sed' instead, as this one is a real 'stream editor'.

sed 's/old/new/g' < fromfile > tofile && mv tofile fromfile

You asked for a 'one liner', didn't you ? ;-)


Best regards,

Dan
Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Curtis Larson
Trusted Contributor

Re: using ex in commandline for exchanging texts in a file

do something like this:

ex - ${YOUR_FILE} <<-EOCFG
your edit commands
wq
EOCFG
Curtis Larson
Trusted Contributor
Solution

Re: using ex in commandline for exchanging texts in a file

althought a simple:

ex -s +"%s/old/new/g | wq!" filename

should work in this case