Active State Perl with Unix-line endings -- Help

B

Brian & Colleen

I'm using Active State Perl on a WinXP machine. I also run Cygwin and I
decided I wanted a utility to clean my .bash_history file to remove some
commands and duplicate lines.

The line endings are driving me nuts because I think Perl assumes it's on a
Win Platform but bash is writing Unix-style line endings. I keep ending up
with spurious ^M's in my .bash_history. I'm trying to force things with $/
and $\ but it isn't working. As you can tell from the code, I'm new to Perl
so it's probably something really simple.
----------------------------
use strict;

$/ = "\012"; # force use of Unix-style line endings as created
$\ = "\012"; # bash. Strip them on input and restore on output.

my $delstr = qr /exit|cd|ls|rm|cat|pwd|which/ ;
my $oldline = "zzzzzzz";
my $line = "";
open(INFILE,"<.bash_history");
open(OUTFILE,">.bash_history2");

while ($line=<INFILE>){
chomp $line;
if (($line !~ /$delstr/) && ($line ne $oldline)){
print OUTFILE "$line";
$oldline = $line;
}
}
close INFILE;
close OUTFILE;

if (rename ".bash_history2",".bash_history") {
print "Done.";
}
 
B

Brian & Colleen

Good point both Tim and Bob made about using Unix tools to solve Unix
problems. I updated my Cygwin to include Perl and that seems to take care of
the confusion over line endings. What I thought should be a good little
"learning" project turned out to be more educational than I bargained for.

Thanks for the suggestions.

Brian
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top