I'm not sure what these errors are telling me.

The script is supposed to remove dups from .bash_history but not
operate on the last 12 lines... just reprinting them.

./uniqbh.pl
 Copying ./bash_history to ./bash_history-101809_020827
 Unlinking ./bash_history
 Copying /tmp/lPlN3goGnI.tmp ./bash_history 
 Operation "eq": no method found,
        left argument in overloaded package File::Temp,
        right argument has no overloaded magic at
 /usr/lib/perl5/5.8.8/File/Copy.pm line 76, <BASH_HISTORY> line 18151.

------- 8< snip ---------- 8< snip ---------- 8<snip ------- 
#!/usr/bin/perl

use strict;
use warnings;

# my $BashHistory = "$ENV{'HOME'}/.bash_history";
my $BashHistory = "./bash_history";
my $data = '';
my %data;
my $ext = '.tmp';
## Creat an honest to goodness random tmp file name
require File::Temp;
use File::Temp ();

# [HP 101709_220324  Unlink is set to 1 by default .. that
# means it will be unlinked when the object goes out of scope
# so setting it to 0 means I have to delete (unlink) it manually        
my $tmpfile = new File::Temp( UNLINK => 0, SUFFIX => '.tmp' );

open(TMPFILE,">$tmpfile") or die "Can't open $tmpfile: $!";
    
## Keep this many lines from being processed, but do include them
## in the final file.
my $KeepLines = 12;
my $ProcessLines = (count_lines($BashHistory) - $KeepLines);

#####     BEGIN Body     #####     #####     ##### 
open(BASH_HISTORY,"<$BashHistory")or die "Can't open $BashHistory: $!";
while (<BASH_HISTORY>) {
    chomp;
  ## Process all but $KeepLines
  if($. <= $ProcessLines){
    ## finds unique input lines and prints them to a separate file
    if ($data{$_}++ == 0) {
      print TMPFILE $_ . "\n";
    }
  }elsif($. > $ProcessLines){
    print TMPFILE  $_ . "\n";   
  }
}
close(TMPFILE);

use File::Copy;

print "Copying $BashHistory to ".$BashHistory."-". PaddedDateStr()."\n"; 
copy( $BashHistory, $BashHistory . "-" . PaddedDateStr() )
                or die "Copy failed: $!";

print "Unlinking $BashHistory\n";
unlink $BashHistory;

print "Copying $tmpfile $BashHistory \n";
copy( $tmpfile, $BashHistory ) 
                or die "Copy failed: $!";

print "Unlinking $tmpfile\n";
unlink $tmpfile;

#####     BEGIN sub functions     #####     ##### 
sub count_lines {
    my $file = shift;
    open my $fh, "<", $file
        or die "could not open $file:$!";
    my $lines = 0;
    $lines++ while <$fh>;
    return $lines;
}

sub PaddedDateStr {
my ($mon,$mday,$year,$hour,$min,$sec,$wday) = (localtime(time))[4,3,5,2,1,0,6];
#    $year += 1900; ## prints 2005 
    $year -= 100;  ## prints 05 (with %02d)
    $mon  += 1;
my  $PaddedDateStr = sprintf "%02d%02d%02d%s%02d%02d%02d", 
$mon,$mday,$year,"_",$hour,$min,$sec;
    return $PaddedDateStr;
}



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to