vi / ex : Resume on Pattern
Fail
Example situation:
data.txt contains something like:
abcdefghijklmno
blahblah blahblah blah
xxxxxxxxxxxxxxxx
and then vi.script contains something like:
:%s/abc/ABC/g
:%s/HORK//g
:%s/blahblah//g
:wq
and is initiated via:
vi data.txt < vi.script
This works just fine as long as the search pattern
in all statements do exist. If one of them doesn't exist, then vi quits
(returning 1) without processing any further commands.
I want it to process all the commands, and if it runs
into a pattern match that doesn't exist, I want it to just resume on the
next line and keep going. So as in my example above, once if failed to
find "HORK" in the file, I want vi to just continue on to look for "blahblah"
and so forth instead of just quitting.
--------------------------------------------------------------------------------
Using a Unix 'here' document seems to work, at least on
my Solaris 7 box:
vi data.txt <<MSG
:%s/abc/ABC/g
:%s/HORK//g
:%s/blahblah//g
:wq
MSG
--------------------------------------------------------------------------------
It made no difference on OSF/1.
I did discover something interesting though: I briefly
saw the stdout as the program was running, and I saw that vi printed the
following notification: "press enter to continue". This is not seen when
stdin is connect to a terminal; instead "Substitute pattern match failed"
is what is seen. So I tried putting at the end of the non-matching line:
^V^M (key-escape,enter-key) and it successfully bypassed. But when I put
it on lines that do have a match (and thus would not need the escaped enter
key, the script fails again. Of course I can't just put escapes on only
the problem lines because the point is I don't know which patterns will
not be found.
--------------------------------------------------------------------------------
Many versions of vi/ex return this particular
warning message.
You probably would be better off (and
safer) using the sed utility i.e.
sed -f vi.sed data.txt > data.out
where vi.sed contains
s/abc/ABC/g;
s/HORK//g;
s/blahblah//g;
See Also
Unix
Administrator Career Path
Have a Unix Problem
Do
you have a UNIX Question?
Unix Books :-
UNIX Programming,
Certification, System Administration, Performance Tuning Reference Books
Return to : - Unix
System Administration Hints and Tips
(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.
Information used on this site is at your own risk.
All product names are trademarks of their respective
companies.
The site www.gotothings.com is in no way affiliated with
or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.
|