VI Search and Replace problem help...

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions VI Search and Replace problem help...
# 1  
Old 08-11-2011
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,
Code:
:%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
Code:
:%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

4. 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).

Last edited by zxmaus; 08-11-2011 at 11:56 PM.. Reason: added code tags
# 2  
Old 08-12-2011
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.
# 3  
Old 08-12-2011
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
# 4  
Old 08-12-2011
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:
Code:
:ADDRESS CMD

Btw, try one cmd with a such address :/^...|\1$/ s... I'm not sure whether it will work but you can check.
# 5  
Old 08-16-2011
Quote:
Originally Posted by kbreitsprecher
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
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 Smilie.

Code:
:%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.

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Large search replace using sed results in memory problem.

I have one big file of size 9GB (big_file.txt). This big file has sentences and paragraphs like any usual English document. I have another file consisting of replacement strings for sed to use. The file name is replace.sed and each entry in one line looks like this: s/\<shout\>/shout/g s/\<b is... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

2. Shell Programming and Scripting

Nested search in a file and replace the inner search

Hi Team, I am new to unix, please help me in this. I have a file named properties. The content of the file is : ##Mobile props east.url=https://qa.east.corp.com/prop/end west.url=https://qa.west.corp.com/prop/end south.url=https://qa.south.corp.com/prop/end... (2 Replies)
Discussion started by: tolearn
2 Replies

3. UNIX for Dummies Questions & Answers

Search and replace problem

Hi, I am looking for bash or awk script to solve the following. Input File 1: >Min_0-t10270-RA|>Min_0-t10270-RA protein AED:0.41 eAED:0.46 QI:0|0|0|0.25|1|1|4|0|190 MIGLGFKYLDTSYFGGFCEPSEDMNKVCTMRADCCEGIEMRFHDLKLVLEDWRNFTKLST EEKRLWATPAAEDFF >Min_0-t10271-RA|>Min_0-t10271-RA protein... (5 Replies)
Discussion started by: Fahmida
5 Replies

4. Programming

Binary Search Tree Search problem

I am writing code for a binary search tree search and when I compile it i am getting strange errors such as, " /tmp/ccJ4X8Xu.o: In function `btree::btree()': project1.cpp:(.text+0x0): multiple definition of `btree::btree()' " What does that mean exactly? tree.h #ifndef TREE_H #define... (1 Reply)
Discussion started by: meredith1990
1 Replies

5. Shell Programming and Scripting

perl search and replace - search in first line and replance in 2nd line

Dear All, i want to search particular string and want to replance next line value. following is the test file. search string is tmp,??? ,10:1 "???" may contain any 3 character it should remain the same and next line replace with ,10:50 tmp,123 --- if match tmp,??? then... (3 Replies)
Discussion started by: arvindng
3 Replies

6. UNIX and Linux Applications

GNU sed - Search and Replace problem

Hi, The following code loops through every file with an error extension and then loops through all XML files in that directory and replaces the target character @ with / . The problem I have is that if there is more than one occurance of @ in each individual file it doesn't replace it. Any... (2 Replies)
Discussion started by: Fishn
2 Replies

7. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

8. Shell Programming and Scripting

Problem with sed (search/replace)

Hi, In a file FILE, the following lines appear : WORD 8 8 8 ANOTHERWORD blabla ... Directly in the prompt, if I type $sed '/WORD/s/8/10/g' FILE it replace the 8's by 10's in file : $cat FILE WORD 10 10 10 ANOTHERWORD blabla ... (9 Replies)
Discussion started by: tipi
9 Replies

9. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies

10. UNIX for Dummies Questions & Answers

vi search & replace ... having '/' in string - problem.

I want to carry out search & replace for the paths mentioned in the file with the help of vi. 'abc/' to be replaced by 'abc/data' When I use command in vi as below - %s/abc//abc/data/g it gives me an error. How we should deal with '/' part in string for vi search & replace? ... (6 Replies)
Discussion started by: videsh77
6 Replies
Login or Register to Ask a Question