public
Last active

Clean unneeded entries from shell history files

  • Download Gist
clean_history.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
#!/usr/bin/perl
 
use 5.014;
use warnings;
use autodie;
use utf8::all;
use IO::All;
 
die 'No input file specified.' if ( $#ARGV == -1 );
 
my @patterns = map {qr/^$_/xms} @{
[ '(?:mk|rm)dir\ ',
'(?:acroread|epdfview)\ ',
'ls$',
'cd$',
'exit$',
'clear$',
'df$',
'pwd$',
'su$',
'du$',
'ls[ao]$',
'dus$',
'firefox$',
'cal$',
';$',
'blender$',
'[.]/blender$',
'date$',
'export$',
'htop$',
'lpstat$',
'ps$',
'firefox\ &',
'gimp-2\.6\ \&',
'ls\ ',
';s',
'cd\ ',
'rm\ ',
'tar\ ',
'df\ ',
'df\ \.',
'du\ ',
'wget',
'mv\ ',
'file\ ',
'source\ ',
'gv\ ',
'm\ ',
'pc[ ]?',
'unzip',
'geeqie',
'man\ ',
'scite',
'chmod\ ',
'pgrep\ ',
'pkill\ ',
'slocate\ ',
'7z\ ',
'cal\ ',
'cat\ ',
'cp\ ',
'eject\ ',
'touch\ ',
'which\ ',
'nano\ ',
'mediainfo\ ',
'opera\ ',
'sftp\ ',
'xterm$',
'google\-chrome',
'uname\ -a',
 
# dev stuff
'git[ ](?:status|commit|log|diff|fetch|checkout|init|pull|push)$',
'git[ ](?:diff|blame)[ ]',
'cover[ ]-(?:test|delete)$',
'cover[ ]--(?:test|delete|help)$',
'git\ add\ \.$',
'make$',
'make\ test$',
'prove$',
'cmake$',
'cover$',
'cpan$',
'perl\ Makefile[.]PL',
'corelist\ ',
'[.]/configure\s*$',
'make[ ]clean$',
'cvs\ status$',
're[.]pl$',
'diff\ ',
'[.]/perltest[.]pl',
'perltest.pl',
'perl\ --version',
 
#misc
'vlc\ ',
'vlc$',
'mplayer\ ',
# I trimmed the rest out because they were too specific
]
};
 
foreach my $fname (@ARGV) {
my @content = io($fname)->slurp;
foreach (@content) { s/^\s+//xms; s/[ ]+$//xms; }
 
open my $fh, '>', $fname . '_clean';
open my $junk, '>', $fname . '_junked.log';
 
LINE: foreach my $line (@content) {
foreach my $re (@patterns) {
if ( $line =~ $re ) {
print {$junk} $line;
next LINE;
}
}
print {$fh} $line;
}
 
close $fh;
close $junk;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.