Re: Help with string replacement



On Feb 20, 1:49 pm, Ala Qumsieh <nore...@xxxxxxxxxxx> wrote:
ame...@xxxxxxx wrote:

Hi,

I'm trying to use the -pi switches to replace some strings in a file.
However, I'm getting some very strange results.

My test file looks like this:

daily_rank.sh|22:30|22:31|22:35|Y
dart_process.sh|17:45|17:45|14:46|Y
prft_trck.sh|7:30|7:30|7:46|Y

Here is my command line:

perl -pi -e "s/$x/$y/;" job_control

$x = daily_rank.sh|22:30|22:31|22:35|Y
$y = daily_rank.sh|22:30|14:13|22:35|N

After the Perl command executes, my file looks like this:

daily_rank.sh|22:30|14:13|22:35|N|22:30|22:31|22:35|Y
dart_process.sh|17:45|17:45|14:46|daily_rank.sh|22:30|14:13|22:35|N
prft_trck.sh|7:30|7:30|7:46|daily_rank.sh|22:30|14:13|22:35|N

This is not what I expected. I excpected just the first line to be
replaced.

This may have to do with quotes? Or maybe it has to do with the pipe
being a special character? But, I've been at it for hours and was
looking for a bit of help?

Yes, the pipe. You need to quotemeta-it:

s/\Q$x/$y/

checkout 'perlre' and '-f quotemeta' for more info.

--Ala


# Simple but simple example use of the \Q quote meta. Just 2 cents.
$foo = "a b c ";
print "\Q$foo";
$data = "\Q$foo";
print "\nD $data \n";
$foo = '$ % abc \ ';
$data = "\Q$foo";
print "\nD $data \n";

# jb

.



Relevant Pages

  • Re: bug or unknown feature in tsql/somewhere?
    ... Really strange thing happened again. ... Code that does not seem to have anything wrong just wouldn't compile when ... this special character among the code that was not visible in SQL analyser. ... Replaced the line formating and the blanks. ...
    (microsoft.public.sqlserver.programming)
  • Re: Convert user input to wdKey constants?
    ... but something strange happens: ... in HTML) ... They all get an extra "special character". ...
    (microsoft.public.word.vba.general)
  • Re: Special charaters are displayed in inverted order by CTreeCtrl
    ... strange thing is, that the problem only occurs, when a special character at ... GetStringmethod returns a 'const TCHAR *', ... Sorry for the typo: I meant const_cast: ...
    (microsoft.public.vc.mfc)