Help with string replacement

A

amerar

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!
 
A

Ala Qumsieh

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
 
J

johngnub

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top