VI Search and Replace problem help...

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:
    Give the vi command for replacing all occurances of the string "DOS" with the string "UNIX" in the whole document that is currently being edited. what are the commnads for replacing all occurances of the strings "DOS" and "WINDOWS" with the string "UNIX" from all lines that start or end with these strings in the document being edited?

  2. Relevant commands, code, scripts, algorithms:

  3. The attempts at a solution (include all code and scripts):
    Part 1 was easy,

:%s/DOS/UNIX/g

But part two is what I am having trouble with. I can replace all instances at the end of the line, but not the instances IN the line that end with that word. My best solutions are

:%s/^\(DOS\|WINDOWS\)/UNIX/g
:%s/\(DOS\|WINDOWS\)$/UNIX/g

But they only replace the test "DOS" or "WINDOWS" at the beginning or end of the line with "UNIX"

So "DOS and WINDOWS are better than OS/2" Would become,
"UNIX and WINDOWS are better than OS/2" when it should be,
"UNIX and UNIX are better than OS/2"

I've tried google and the text book doesn't help at all with this. I know I have to be close but I just can't figure it out.

Thanks

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Athabasca University, Calgary(I'm in Thunder Bay), Canada, Dr. Mahmoud Abaza, COMP315

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Just a hint. "%" is an address for vi command to operate on. It is the whole file, but this address can be a regex (or line numbers).
You are doing very well and I don't want just to give you a solution.

If i am not mistaken the % sign will make vi search every line in the file.

I have tried this, and it works, (I found another example in a page somewhere) but can someone break it down for me? I don't follow the first part of it.
:g/^\(DOS\|WINDOWS\)/s/\(DOS\|WINDOWS\)/UNIX/g
:g/\(DOS\|WINDOWS\)$/s/\(DOS\|WINDOWS\)/UNIX/g

I don't understand the :g at the beginning or why the s is after the line beginning with section....

Thanks

You do not need "g(lobal)" command here. You just need an address of lines for your substitution. Just remove it and you get an usual ex command pattern:

:ADDRESS CMD

Btw, try one cmd with a such address :/^...|\1$/ s... I'm not sure whether it will work but you can check.

Check this out. The :g just means global.

Vi Cheat Sheet

I broke this up into separate cases and have a simpler solution to do this all at once. I figured it out after studying your ideas :).

:%s/\(^DOS\|DOS$\|^WINDOWS\|WINDOWS$\)/UNIX/g

---------- Post updated at 04:39 AM ---------- Previous update was at 04:16 AM ----------

I read what you said closer and fixed it. Since you have so many requirements you gotta break them up into separate cases.

:%s/\(^DOS\|DOS$\|DOS\|^WINDOWS\|WINDOWS$\|WINDOWS\)/UNIX/g