Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Including special characters in a search and replace

by Chady (Priest)
on Jan 15, 2005 at 06:53 UTC ( #422470=note: print w/ replies, xml ) Need Help??


in reply to Including special characters in a search and replace

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.


Comment on Re: Including special characters in a search and replace
Download Code
Re^2: Including special characters in a search and replace
by wudaben (Initiate) on Jan 16, 2005 at 05:09 UTC
    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!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://422470]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (10)
As of 2014-12-25 05:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (159 votes), past polls