Re: Help with string replacement
- From: "johngnub" <johngnub@xxxxxxx>
- Date: 20 Feb 2007 13:23:28 -0800
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
.
- References:
- Help with string replacement
- From: amerar@xxxxxxx
- Re: Help with string replacement
- From: Ala Qumsieh
- Help with string replacement
- Prev by Date: Re: Tool to create Perl based scripts
- Next by Date: Re: Combining M-F school week and 6 day class cycle
- Previous by thread: Re: Help with string replacement
- Next by thread: Combining M-F school week and 6 day class cycle
- Index(es):
Relevant Pages
|