NAME
Bash::History::Read - Utility to read bash history file entries
VERSION
This document describes version 0.04 of Bash::History::Read (from Perl distribution Bash-History-Read), released on 2015-11-07.
SYNOPSIS
From script:
use Bash::History::Read qw(parse_bash_history_file);
my $res = parse_bash_history_file("$ENV{HOME}/.bash_history");
Sample result:
[
[undef, "some-command\n"],
[1446715184, "du -sm\n"],
[1446715190, "ls -l\n"],
]
From the command-line:
% perl -MBash::History::Read -i.bak -e'each_hist {
$PRINT = 0 if $TS < time()-2*30*86400; # delete old entries
$PRINT = 0 if /foo/; # delete unwanted lines (e.g. matching some regex)
s/(mysql\s+-p)(\S+)/$1******/; # redact sensitive information
}' ~/.bash_history
DESCRIPTION
This module provides utility routines to read entries from bash history file (by default ~/.bash_history
). The format of the history file is dead simple: one line per entry, but when HISTTIMEFORMAT
environment is set, bash will print a timestamp line before each entry, e.g.:
#1374290613
ls -al
#1374290618
less myfile
#1374290635
...
See each_hist
for one routine to let you handle this format conveniently.
FUNCTIONS
parse_bash_history_file([ $path ]) => array
Parse entries from bash history file. If unspecified, $path
will default to HISTFILE
environment variable or $HOME/.bash_history
.
Return an array of entries, where each entry is <[$timestamp, $line]
> and $timestamp
can be undef if entry does not have a timestamp.
each_hist { PERL_CODE }
Will read lines from the diamond operator (<>
) and call Perl code for each history entry. Can handle timestamp lines. This routine is exported by default and is meant to be used from one-liners.
Inside the Perl code, $_
is locally set to the entry content, $TS
is locally set to the timestamp (and changes to this variable is ignored, except when you undefine the variable, which will remove the timestamp from output), $PRINT
is locally set to 1. If $PRINT
is still true by the time the Perl code ends, the entry (along with its timestamp) will be printed. So to remove a line, you can set $PRINT
to 0 in your code. To modify content, modify the $_
variable.
HOMEPAGE
Please visit the project's homepage at https://metacpan.org/release/Bash-History-Read.
SOURCE
Source repository is at https://github.com/perlancar/perl-Bash-History-Read.
BUGS
Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Bash-History-Read
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
AUTHOR
perlancar <perlancar@cpan.org>
COPYRIGHT AND LICENSE
This software is copyright (c) 2015 by perlancar@cpan.org.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.