CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
String replace

 



sjagannath
New User

Oct 31, 2010, 10:14 AM

Post #1 of 4 (364 views)
String replace Can't Post

Hey,

I am writing this module which parses a log file line by line, process it and writes the processed line to another file. As a part of processing I am replacing every '\' with "\\", the code I have written is not doing what I exactly want it to do.

Here is what I do:
I have my log file: dump.log
Temp output file: temp.txt

dump.log
String\
Another\\String
\\\YetAnotherString

Code:
#!/usr/bin/perl


my $DUMP_FILE = "./dump.log";
open(fdesc, $DUMP_FILE) or die("Could not open \"$DUMP_FILE\"");
print "File open success\n";
while ( defined( $line = <fdesc>) )
{
chomp($line);
print $line . "\n";
$line =~ s/"\\"/"\\\\"/g;
print $line . "\n";
`echo $line >> temp.txt`;
}

Contents of temp.txt:
String
Another\String
\YetAnotherString

I am kinda stumped with it and have been trying to figure it out for a while now but all in vain!

Please, let me know what is that I am doing wrong in my code and why exactly is the output different from what I expect.

Thanks,
Sharath



Code


Code



      
    


BillKSmith
Veteran

Oct 31, 2010, 1:58 PM

Post #2 of 4 (360 views)
Re: [sjagannath] String replace [In reply to] Can't Post

change


Code
    $line =~ s/"\\"/"\\\\"/g;



to


Code
    $line =~ s/\\/\\\\/g;





Note: The whole job can be done with a single-line dos command. Refer to the -i and -p options in perldoc perlrun.


Code
  

perl -ip.bat -e"$line =~ s/\\/\\\\/g" dump.log

Good Luck,
Bill


sjagannath
New User

Oct 31, 2010, 2:11 PM

Post #3 of 4 (358 views)
Re: [BillKSmith] String replace [In reply to] Can't Post

Hey Bill,

Thanks for the reply.
I had already tried it with "$line =~ s/\\/\\\\/g;"

Output of temp.txt:
String\
Another\String
\\YetAnotherString

I rather want it to be

String\
Another\\String
\\\YetAnotherString


Note: The whole job can be done with a single-line dos command. Refer to the -i and -p options in perldoc perlrun.
-- That is not the only operation I am doing on the log files, it is just a part of bigger operation. Anyways thanks good to know new ways of doing things. It might help me some other time.

In Reply To


BillKSmith
Veteran

Oct 31, 2010, 9:31 PM

Post #4 of 4 (348 views)
Re: [sjagannath] String replace [In reply to] Can't Post

I am confused!


Quote

Code
Output of temp.txt:  
String\
Another\String
\\YetAnotherString

I rather want it to be

String\
Another\\String
\\\YetAnotherString





Is this not exactly the same as what you called dump.log in your first post? Please post a sample input and the expected output. Show backslash characters exactly as they appear in the files. i.e. do not add escapes.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives