Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regex behavior question

Status
Not open for further replies.

mwpclark

Programmer
Mar 14, 2005
59
US
Hi

I have 2 regex codes:

This works:
Code:
perl -e "s/\&\#39\;/\'/g" -p -i file.txt

This does not work:
Code:
perl -e "s/\&\#39\;/\'/g" -pi file.txt

The only difference is the -pi versus -p -i.

The -pi version seems to work for other substitutions, just not this one. The only difference there is this is enclosed in double quotes (so that I can use the \').

Can somebody explain why one works and the other does not?

Thanks
Mike
 
generally that is written like this:

perl -pi -e "s/\&\#39\;/\'/g" file.txt

or:

perl -p -i -e "s/\&\#39\;/\'/g" file.txt

The code wrapping quotes are important, double-quotes works on Windows, but not Unix/Linux, single-quotes are used for those. Not sure what a Mac uses. Also on Windows you may need to define what -i is, like '.bac' or whatever.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks Kevin

I have found that double quotes work on linux as well as single, and that when replacing a single quote within the string I have to enclose in double quotes.

But I don't understand why -p -i works with this string but not -pi
 
Might have something to do with the syntax or the operating system but I just don't know.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
When you say it doesn't work... how exactly doesn't it work? Do you get an error message? Does the replacement not occur? Is a backup of the original file created with an unexpected name?

Annihilannic.
 
Both forms seem to work as expected for me (tested under ksh 88, HP-UX 11.11, perl 5.8.0).

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top