wudaben has asked for the wisdom of the Perl Monks concerning the following question:

I'm having some difficulty getting this search and replace correct. The file I'm searching looks like this:
00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,01,00,00,00,02,00,01,00,39,\  00,39,00,39,00,39,00,30,00,30,00,30,00,30,00,00,00,00,00,00,00,00,00,00,00,\ I'm trying to replace the four 39's with a custom number...here is my code for the search:

$line =~ s/39,\\\\r\\n00,39,00,39,00,39/3$fields[0],\\\\r\\n00,3$field +s[1],00,3$fields[2],00,3$fields[3]/g;


I've seperated it down to the carriage return...I've tried \\r, \\n, & \\r\\n with no luck. I do know I can do a replace in Ultraedit with ^p.

Thanks in advance!

Edited by Chady -- added code tags.

Replies are listed 'Best First'.
Re: Including special characters in a search and replace
by Caron (Friar) on Jan 15, 2005 at 06:28 UTC

    Use an index for your array of fields, combined with the /g modifier. This one works:

    #!/usr/bin/perl -w use strict; my $line = "00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,01,00,00,00," ."02,00,01,00,39,00,39,00,39,00,39,00,30,00,30,00,30,00,30,00," ."00,00,00,00,00,00,00,00,00,00,"; my @fields = (100, 101, 102, 103); my $index =0; $line =~ s/\b39\b/$fields[$index++]/g; print $line; __END__ output: 00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,01,00,00,\ 00,02,00,01,00,100,00,101,00,102,00,103,00,30,00,30,00,30, \ 00,30,00,00,00,00,00,00,00,00,00,00,00,

    See perlre for an explanation of the modifiers I have used here.

Re: Including special characters in a search and replace
by Chady (Priest) on Jan 15, 2005 at 06:53 UTC

    It's not really clear what you are trying to do, but here are my assumptions:

    I assume that the above line is an actual line from the file, and depending on how you have read it in, it most probably is getting read line by line, therefore, $line will not contain the concatenated lines together and that's why your regex is not working (besides escaping the \ of \r and \n)... so here's a quick snippet illustrating how to do it (using same regex from Re: Including special characters in a search and replace):

    open IN, "<", "thefile.ext" or die "Cannot open file: $!\n"; my $i = 0; while (my $line = <IN>) { $line =~ s/,39,/,3$fields[$i++],/g; # now what you want to do with $line is up to you } close IN;

    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.
    Chady | http://chady.net/
    Are you a Linux user in Lebanon? join the Lebanese Linux User Group.
      I think you've got it pretty much covered Chady and Caron.

      The lines are a section of hex from a Windows registry file & the 39's are the first four digits of a user's access code to use a specific color laser printer. I'm reading a code in based on username and trying to replace the four 9's with each user's specific code.

      The fact that it's on multiple lines is definately part of my problem. I also was trying to escape my \n characters as well. I didn't realize I could each value without specifying the whole line. Here is my finished output:

      #!/usr/bin/perl open (USERS, "user.txt") or die "Cannot open file user.txt: $!\n"; @users = (<USERS>); close USERS; foreach (@users){ chomp; ($userid,$acode) = split/\|/; if ($userid eq $ENV{"USERNAME"}) { @fields = split //, $acode; open(REG, "template.reg") or die "Cannot open file template.re +g: $!\n"; @hex =(<REG>);; close(REG); open(NEWREG, "> import.reg") or die "Cannot open file import.r +eg: $!\n"; foreach $line (@hex){ my $i = 0; $line =~ s/,39,/,3$fields[$i++],/g; print NEWREG "$line"; } close(NEWREG); } }


      Thanks for the assistance!
Re: Including special characters in a search and replace
by Thilosophy (Curate) on Jan 15, 2005 at 06:14 UTC
    How about this?
    $line =~ s/39/3$fields[0]/; $line =~ s/39/3$fields[1]/; $line =~ s/39/3$fields[2]/; $line =~ s/39/3$fields[3]/;

    Update: This answer is getting voted down a lot. Maybe I am blind today, but could someone point out what is wrong with it?