views:

272

answers:

2

I've scripted up a simple ksh that calls a Perl program to find and replace in files. The passed-in arg is the home directory:

 perl -pi -e 's/find/replace/g' $1/*.html

It works great. However, I'd like to output all the changes to a log file. I've tried piping and redirecting and haven't been able to get it work. Any ideas?

Thanks, Glenn

+8  A: 

Something like this to send all changes to STDERR:

perl -pi -e '$old = $_; s/find/replace/g and warn "${ARGV}[$.]: $old $_"; close ARGV if eof' $1/*.html

Updated: Fixed $. on multiple files.

runrig
Thanks, I'll try it tonight. Just wondering if you can explain the variables? denkfaul's solutions redirects the output to logfile.txt. In your solution I'm not sure which variable is pointing to the logfile. And I'll pose the same question to you that I did to denkfaul regarding using Perl...
"warn" outputs to STDERR, so you can redirect it to a log file just like in denkfaul's answer. "$ARGV" is the name of the current file, "ARGV" is the filehandle, $_ is the current line, $. is the current line number. See "perldoc perlvar" and "perldoc -f eof"
runrig
Excellent. Thanks. And did you get a chance to see the question I was referring to in the second part: 'Would the solution be easier or be more logical if I switched to a native Perl script from a ksh script'?
+3  A: 

You can print to STDERR and redirect just the STDERR output to a file as below:

perl -pi -e 'chomp($prev=$_);s/find/replace/g and print STDERR "$ARGV - $.: $prev -> $_"; close ARGV if eof' $1/*.html 2> logfile.txt

edit: added the filename, and fixed line number display when multiple input files are used

denkfaul
Honestly, I thought of the eof thing, came back here to make an edit, and noticed that you already changed yours :-)
runrig
no worries :)I would've added comments to yours initially, rather than create a similar answer, but I don't yet have the rep :|
denkfaul
Thanks, I'll give it a try tonight.Would the solution be easier or be more logical if I switched to a native Perl script from a ksh script?
@glenwark: For something this simple, it doesn't matter much which way you do it.
runrig