Reply
Thread Tools Search this Thread
peterleinchen's Avatar
Posts: 946 | Thanked: 1,104 times | Joined on Aug 2010 @ Ruhrgebiet, Germany
#31
Originally Posted by misterc View Post
EDIT: well, the whole line searches for fdisk and replaces it with fsck and g does that
Well, after just finishing a well constructed sed command
$(echo $name | sed "s/(operator.)/( `echo $oper | sed 's/\(&\)/\\\&/'` )/")
for replacing some chars, I thought s/re/replace/ would be enough? It is for sed.
But ed (and vi) take s/re/replace/ to just replace in that specific line!?
And s/re/replace/g replaces globally (all text lines)?
 
Posts: 905 | Thanked: 1,723 times | Joined on Dec 2010 @ Dayton, Ohio
#32
Originally Posted by peterleinchen View Post
Well, after just finishing a well constructed sed command
$(echo $name | sed "s/(operator.)/( `echo $oper | sed 's/\(&\)/\\\&/'` )/")
for replacing some chars, I thought s/re/replace/ would be enough? It is for sed.
But ed (and vi) take s/re/replace/ to just replace in that specific line!?
And s/re/replace/g replaces globally (all text lines)?
I should probably just look this up, but off the top of my head: "ed" is a single-line text editor. "vi" is a full-screen text editor based more or less off of "ed", and therefore has line-oriented commands similar to ed.

The "s" command is for search-and-replace. The "s/searchitem/replaceitem" will look for the regular expression "searchitem" and replace it with the regular expression "replaceitem" (at least I think they are regular expressions) in the current line of text. You can add a third slash to provide extra options; the "g" option stands for something like "global", which means to replace every instance of "searchitem" with "replaceitem" in the current line of text. This form of the command should work with both ed and vi.

Since vi is a multi-line editor, it has additional prefix options to specify ranges of lines. (At least, I don't recall these being used in ed.) So, if you want to do a search and replace on every line, you can use the "%" prefix:

%s/searchitem/replaceitem

But that only catches the first instance of searchitem on each line; you still have to add the g if you want to replace every instance on every line:

%s/searchitem/replaceitem/g

You can specifically control the begin and end of a range using two line identifiers separated by a comma. So, to search and replace from lines 5 to 10, do:

5,10s/searchitem/replaceitem/g

If you want to search and replace from the current line to the end of the file, "." means current line, and "$" means end of file:

.,$s/searchitem/replaceitem/g

You can even use the result of a search to define a line. I'll often put a string like "mymark" on the line I want a search and replace to end, move the cursor where I want it to start, and use:

.,/mymark/s/searchitem/replaceitem/g

Anyway, you can do lots of different fun tricks with this system.
 

The Following 2 Users Say Thank You to Copernicus For This Useful Post:
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search

 
Forum Jump


All times are GMT -4. The time now is 06:11 AM.