replacement string in grep

I am wondering what are the special characters in the replacement ( second string) string in when I use grep in TextWrangleor Perl or other such substitutions.


Example:

perl -0777pe 's/\r\n/\n/g'


Other than the \ in the second string ( the \n part , what are special characters. What must I escape besides \ ???


Robert

Posted on Jul 4, 2012 9:01 AM

Reply
2 replies

Jul 4, 2012 9:19 AM in response to rccharles

any regexp special character that you want to use literally must be escaped. E.g. if you want to search for a literl period you need to use \. (because a period is a regexp wildcard) and if you want to use a literal bracket '[' you have to use \[ (because brackets are regexp character range specifiers). In your particular case it doesn't look like anything else needs to be escaped.


if you're learning regexp, look through this tutorial. More than you'll ever want to know...

Jul 4, 2012 9:24 AM in response to rccharles

TextWrangler -> Help -> TextWrangler Help -> Grep -> Searching with Grep, then scroll down to "Writing Replacement Patterns"


Perl regular expressions replacement fields accept variable names


s/old/${new}/


Can substitute patterns found in the search side


s/ab(.*)yz/pq${1}rs/


There is most likely a world of stuff you can do with Perl, but those are the ones I tend to use mostly. You might find this useful <http://docstore.mik.ua/orelly/perl3/lperl/ch09_06.htm> as well as other Google searchs for "Perl Substitutions"

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

replacement string in grep

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.