[prog] replacement strings

Miriam English mim at miriam-english.org
Mon May 24 03:03:33 UTC 2010


Thanks Sam. I mainly use regex patterns for TextPad. Some Linux text 
editors like geany have usable, though often pretty feeble regular 
expressions. My introduction to regular expressions was way back in the 
Amiga days.

Each time I try to learn to use vi/vim I get bogged down with the 
incredibly steep learning curve, and the fact that its command are so 
different from everything else. Not its fault of course; it existed way 
before the general agreement on user interface standards that gave us 
things like ctrl-c for copy, ctrl-x for paste, and so on. Nevertheless 
it means every time my learning is interrupted I end up having to go 
back almost to square one. Cream is a set of scripts that sit on top of 
vim modify it so that it does largely conform to those standards. I 
guess it's time I took another look at it. This ability to substitute 
various items in the search expression is very powerful incentive.

I hadn't realised sed could do this. More learning I need to do. :)

Perl. I should have thought of that. Perl seems capable of *anything* 
related to string manipulation. I need to sit down and learn it properly 
too. I used to use it years ago, but my knowledge was incomplete and it 
was a long time ago.

Damn. So many things to learn and so little time.

Thank you very much for the pointers and examples.

	- Miriam

Sam Watkins wrote:
> On Mon, May 24, 2010 at 09:37:30AM +1000, Miriam English wrote:
>> Does anybody know of anything (editor or shell command) which can find  
>> and replace patterns like the following. I don't really care about  
>> particular syntax, just so long as something exists that does the job.
>>
>> search pattern: \(foo[0-9]\)\(b[au]r\)
>> replace pattern: \2 \1
>>
>> This searches for "foo" followed by a digit, then "bar" or "bur", then  
>> replaces it with what it found, but in a different order -- in this case  
>> "bar" or "bur" followed by "foo" and the digit that came after it.
> 
> You can do this with vim, emacs, sed, perl and many other tools.
> 
> example in vim:
> 
>   :%s/\(foo[0-9]\)\(b[au]r\)/\2 \1/gc
> 
> The "c" at the end is for "confirm", if you don't want that you can omit the
> "c", or press "a" for "all" at the confirm prompt.
> 
> sed has the same syntax as vim.
> 
>   sed 's/\(foo[0-9]\)\(b[au]r\)/\2 \1/g' < infile > outfile
> 
> perl has a slightly different syntax:
> 
>   perl -pe 's/(foo[0-9])(b[au]r)/$2 $1/g' < infile > outfile
> 
> I don't recall how to do it in emacs.
> 
> It surprises me that you would know regexp syntax but not know how to use
> regexps in any of these tools!  What do you use regexps for if not with these
> tools?
> 
> Sam
> 
> 

-- 
If you don't have any failures then you're not trying hard enough.
  - Dr. Charles Elachi, director of NASA's Jet Propulsion Laboratory
-----
Website: http://miriam-english.org
Blog: http://miriam_e.livejournal.com


More information about the Programming mailing list