From: amerar on

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?

Thanks!

From: Ala Qumsieh on
amerar(a)iwc.net 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

From: johngnub on
On Feb 20, 1:49 pm, Ala Qumsieh <nore...(a)invalid.net> wrote:
> ame...(a)iwc.net 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