Help with string replacement

Discussion in 'Perl Misc' started by amerar@iwc.net, Feb 20, 2007.

  1. Guest

    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!
     
    , Feb 20, 2007
    #1
    1. Advertising

  2. Ala Qumsieh Guest

    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
     
    Ala Qumsieh, Feb 20, 2007
    #2
    1. Advertising

  3. johngnub Guest

    On Feb 20, 1:49 pm, Ala Qumsieh <> wrote:
    > 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
     
    johngnub, Feb 20, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mladen Adamovic
    Replies:
    0
    Views:
    767
    Mladen Adamovic
    Dec 4, 2003
  2. Mladen Adamovic
    Replies:
    3
    Views:
    14,736
    Mladen Adamovic
    Dec 5, 2003
  3. Chris Nevill

    Regex String Replacement

    Chris Nevill, Jan 31, 2004, in forum: Java
    Replies:
    5
    Views:
    482
    Chris Nevill
    Jan 31, 2004
  4. qazmlp
    Replies:
    5
    Views:
    712
    Michael Dunn
    Apr 7, 2004
  5. ridcully
    Replies:
    4
    Views:
    134
Loading...

Share This Page