Computing.Net > Forums > Unix > vi / ex : resume on pattern fail

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

vi / ex : resume on pattern fail

Reply to Message Icon

Name: Anagram
Date: December 15, 2003 at 10:22:09 Pacific
OS: OSF/1
CPU/Ram: Alpha
Comment:

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.

Any clues?



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: December 15, 2003 at 11:26:25 Pacific
Reply:

Hi:

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



0

Response Number 2
Name: Anagram
Date: December 15, 2003 at 12:09:16 Pacific
Reply:

I thank you for your suggestion, nails. Alas, 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.



0

Response Number 3
Name: Finnbarr P. Murphy
Date: December 16, 2003 at 12:14:44 Pacific
Reply:

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;

- Finnbarr


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: vi / ex : resume on pattern fail

vi \ ex : Substitute with newline? www.computing.net/answers/unix/vi-ex-substitute-with-newline/6485.html

contextual GREP in Unix www.computing.net/answers/unix/contextual-grep-in-unix/6011.html

OR operator for searching patterns www.computing.net/answers/unix/or-operator-for-searching-patterns/7870.html