1

in Unix, what I want to do is "history | grep keyword", just because it takes quite some steps if i wanna grep many types of keywords, so I want it to be automation, which I write a Perl script to do everything, instead of repeating the commands by just changing the keyword, so whenever I want to see those certain commands, I will just use the Perl script to do it for me.

The keyword that I would like to 'grep' is such as source, ls, cd, etc. It can be printed out in any format, as long as to know how to do it.

Thanks! I appreciate any comments.

1
  • 2
    Why use a perl script to automate a unix command? Why not a shell script? Or a simple alias. Though all you would save is writing "history | grep", so the way I see it, the value is limited.
    – TLP
    Jul 12, 2011 at 12:55

4 Answers 4

2

modified (thanks to @chas-owens)

 #!/bin/perl
 my $searchString = $ARGV[0];
 my $historyFile = ".bash.history";
 open FILE, "<", $historyFile or die "could not open $historyFile: $!";
 my @line = <FILE>;
 print "Lines that matched $searchString\n";
 for (@lines) {
      if ($_ =~ /$searchString/) {
           print "$_\n";
      }
 }

original

 #!/bin/perl
 my $searchString = $ARGV[0];
 my $historyFile = "<.bash.history";
 open FILE, $historyFile;
 my @line = <FILE>;
 print "Lines that matched $searchString\n";
 for (@lines) {
      if ($_ =~ /$searchString/) {
           print "$_\n";
      }
 }

to be honest ... history | grep whatever is clean and simple and nice ; )

note code may not be perfect

1
  • 3
    The two argument version of open is dangerous. Always use the three argument version: my $historyFile = ".bash_history"; open my $fh, "<", $historyFile or die "could not open $historyFile: $!"; Jul 12, 2011 at 9:23
1

because it takes quite some steps if i wanna grep many types of keywords

history | grep -E 'ls|cd|source'

-P will switch on the Perl compatible regular expression library, if you have a new enough version of grep.

0

This being Perl, there are many ways to do it. The simplest is probably:

#!/usr/bin/perl

use strict;
use warnings;

my $regex = shift;
print grep { /$regex/ } `cat ~/.bash_history`;

This runs the shell command cat ~/.bash_history and returns the output as a list of lines. The list of lines is then consumed by the grep function. The grep function runs the code block for every item and only returns the ones that have a true return value, so it will only return lines that match the regex.

This code has several things wrong with it (it spawns a shell to run cat, it holds the entire file in memory, $regex could contain dangerous things, etc.), but in a safe environment where speed/memory isn't an issue, it isn't all that bad.

A better script would be

#!/usr/bin/perl

use strict;
use warnings;

use constant HISTORYFILE => "$ENV{HOME}/.bash_history";

my $regex = shift;

open my $fh, "<", HISTORYFILE
    or die "could not open ", HISTORYFILE, ": $!";

while (<$fh>) {
    next unless /$regex/;
    print;
}

This script uses a constant to make it easier to change which history file it is using at a latter date. It opens the history file directly and reads it line by line. This means the whole file is never in memory. This can be very important if the file is very large. It still has the problem that $regex might contain a harmful regex, but so long as you are the person running it, you only have yourself to blame (but I wouldn't let outside users pass arguments to a command like this through, say a web application).

2
  • Thanks for the comment, but in my home directory the history is kept in ".history" file instead of ".bash_history", and the ".history" file i have do not update the latest commands that i had run until the xterm is closed or need to type "history -S" to update it.
    – keifer
    Jul 13, 2011 at 2:17
  • history is not a program, it is a part of the shell. This means it is nearly impossible for Perl to get at the contents that are still in memory. Running and piping into a program to do the search is pretty much your only answer. A simple example of this would be history | perl -ne 'print if /\b(?:ls|source|cd)\b/'. This will print only the lines that match the words ls, source, and cd. Jul 13, 2011 at 13:51
0

I think you are better off writing a perlscript which does you fancy matching (i.e. replaces the grep) but does not read the history file. I say this because the history does not appear to be flushed to the .bash_history file until I exit the shell. Now there are probably settings and/or environment variables to control this, but I don't know what they are. So if you just write a perl script which scanns STDIN for your favourite commands you can invoke it like

history | findcommands.pl

If its less typing you are after set up a shell function or alias to do this for you.

As requested by @keifer here is a sample perl script which searches for a specified (or default set of commands in your history). Onbiously you should change the dflt_cmds to whichever ones you search for most frequently.

#!/usr/bin/perl
my @dflt_cmds = qw( cd ls echo );
my $cmds = \@ARGV;
if( !scalar(@$cmds) )
{
    $cmds = \@dflt_cmds;
}
while( my $line = <STDIN> )
{
    my( $num, $cmd, @args ) = split( ' ', $line );
    if( grep( $cmd eq $_ , @$cmds ) )
    {
        print join( ' ', $cmd, @args )."\n";
    }
}
2
  • hey, thanks for the comments. Is there any reference for "history | perl_script" ? I do not how what must included in the script.
    – keifer
    Jul 12, 2011 at 8:16
  • @keifer Added a sample perl script for you. Just checks for commands, not arguments
    – Sodved
    Jul 12, 2011 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.