Friday, June 5, 2009

My faster little find script

I have been using this script for a long time. It certainly could be improved but it has proved invaluable.

##### FILE: find.pl #####


#!/usr/local/bin/perl -w
use strict;
use Getopt::Long;
use Pod::Usage;

my $cmdLine = "rerunRegressionResults.pl @ARGV \n";

my $dateStamp=`date +'%Y-%m-%d-%H%M'`; chomp($dateStamp);
my $output = "";
my $help = 0;
my $grep = "";
my $displayOnly = 0;
my $noGrep = 0;
my $noVim = 0;
my $vimCmd = "";
my $dryRun = 0;
my $cmd = "";
my @fileNames;
my $replaceWithSpace = "~"; # Default is to replace all ~ with spaces.
my $arg = "";
my $verbosity = "";
my $endFind = "";
my @strings;
my $grepArg = "";
my $findRslt = "";
my $findRsltFound = 0;
my $endGrep = "";
my @dirs;
my @findRslts;
my @nots;
my @ands;
my @prunes;
my @sqlResults;
my @outputList;

my $ok = GetOptions(
'displayOnly|do' => \$displayOnly,
'noVim|nv' => \$noVim,
'vimCmd|vc=s' => \$vimCmd,
'dryRun|dr' => \$dryRun,
'dirs=s@' => \@dirs,
'output=s' => \$output,
'endGrep|eg=s' => \$endGrep,
'fileNames|fn=s@' => \@fileNames,
'noGrep|ng' => \$noGrep,
'arg=s' => \$arg,
'verbosity=s' => \$verbosity,
'endFind|ef=s' => \$endFind,
'strings=s@' => \@strings,
'grepArg|ga=s' => \$grepArg,
'nots=s@' => \@nots,
'ands=s@' => \@ands,
'prunes=s@' => \@prunes,
'replaceWithSpace=s' => \$replaceWithSpace,
'help' => \$help
);
if ( !$ok ) { die; }

pod2usage(1) if $help;

push ( @strings , @ARGV );
if ( ( @strings == 0 ) && ( @fileNames == 0 ) ) { die " NO SEARCH STRING OR FILE NAME GIVEN!!! "; }

# Decide the defaults:
if ( @fileNames == 0 ) {
$fileNames[0] = "*";
} else {
if ( @strings == 0 ) { $strings[0] = $fileNames[0]; }
}
if ( @dirs == 0 ) {.
$dirs[0] = "$ENV{'PWD'}";.
}
if ( $vimCmd eq "" ) { $vimCmd = "/usr/local/bin/gvim"; }
if ( $arg eq "" ) { $arg = ""; }
if ( $endFind eq "" ) { $endFind = ""; }
#if ( $grepArg eq "" ) { $grepArg = " -n -I -i -C 3 "; }
if ( $grepArg eq "" ) { $grepArg = " -n -I -i "; }
if ( $output eq "" ) { $output = "~/tmp/find_$strings[0].rslt" }

# Start to build up the find argument

foreach ( @prunes ) { $arg .= " -not \\( -type d -name \"*$_*\" -prune \\) " }
foreach ( @ands ) { $arg .= " -name \"*$_*\" " }
foreach ( @nots ) { $arg .= " -not -name \"*$_*\" " }
if ( @fileNames > 1 ) { $arg .= " \\( "; }
foreach my $index (0..$#fileNames) {
if ( $index > 0 ) { $arg .= " -o "; }
$arg .= " -name \"*$fileNames[$index]*\" ";
}
if ( @fileNames > 1 ) { $arg .= " \\) "; }

# Create the comands and execute
foreach my $string ( @strings ) {
if ( $replaceWithSpace ne "" ) { $string =~ s/$replaceWithSpace/ /g; }
$cmd = "find @dirs $arg $endFind -follow ";
$cmd .= " -print0 | xargs -0 grep $grepArg '$string' $endGrep" unless $noGrep;
print " $cmd \n" unless $verbosity eq "quiet";
$findRslt = qx"$cmd" unless ( $dryRun );
if ( $findRslt =~ m/[a-z]/i ) {
$findRsltFound++;
push ( @outputList , $findRslt );
}
}

if ( ( $findRsltFound < 1 ) && ( ! $dryRun ) ) {.
die " !ERROR! ---> \"$strings[0]\" not found in dir(s) @dirs \n";.
}

# Generate my output
if ( $displayOnly ) {
print @outputList;
} else {
open OUT_FILE, "> $output" or die "cant open $output : $!";
print OUT_FILE @outputList;
close OUT_FILE;
#print @outputList;
unless ( $noVim ) {
my $cmd3;
my $gvimSearch = "";
# The search +/ does not work if using --remote in vim!!! SUCKS
$gvimSearch = " +\"/$strings[0]\" " unless ( $vimCmd =~ m/remote/ );
$cmd3 = "csh -c \"$vimCmd $output $gvimSearch \"";
print " $cmd3 \n";
system("$cmd3") == 0 or die "system $cmd3 failed: $?" unless ( $dryRun );
}
}


__END__

=head1 NAME

find.pl help

=head1 SYNOPSIS

find [options]
Options:
-help This help message
-dirs Directory to search*
-output Alternative output filename
-type Find's type (default = "-type f")
-endGrep|eg Additional grep arguments
-fileNames|fn File Name (default = "*")
-arg Additional find Arguments
-endFind|ef Additional find Arguments for the end of find command
-strings Strings grep will use to search*
-grepArg|ga Alternative grep arguments (default = " -n -I -i -C 3 ")
-nots Additional find logic*
-ands Additional find logic*
-prunes Trims directories from search
-displayOnly|do Does not generate a file
-fno Search for the file name only (no grep)
-noVim|nv Do not open output file with vim
-dryRun|dr Run the script but print the commands do not actually run them

Examples:

find.pl stringtofind
find.pl -fn filetofind stringtofind
find.pl -string stringtofind -fa "-maxdepth 1"
find.pl -s stringtofind -fa "-maxdepth 1"
find.pl stringtofind -fa "-maxdepth 1"
find.pl stringtofind -prune directorynottosearch
find.pl stringtofind -not log
find.pl string_with_spaces -replaceWithSpace _

=cut

Faster editting of linux script files

If you are like me you want to open your scripts quickly without having to find where they are and then opening it up. Use my little vich.pl script to automatiically find and open the script with one command:

##### FILE: vich.pl #####


#!/usr/local/bin/perl -w
use strict;
my $whichOutput = qx` which $ARGV[0]; `;
$whichOutput =~ s/\s+$//;
stat($whichOutput);
print "Readable\n" if -r _;
print "Writable\n" if -w _;
print "Executable\n" if -x _;
print "Setuid\n" if -u _;
print "Setgid\n" if -g _;
print "Sticky\n" if -k _;
print "Text\n" if -T _;
print "Binary\n" if -B _;
if ( ( $whichOutput =~ m/^\// ) && ( -T $whichOutput ) ) {
qx`gvim -geometry 100x50 $whichOutput `;
} else {
my $myoutput = "~/tmp/vich.out";
open OUT_FILE, "> $myoutput" or die "cant open $myoutput : $!";
print OUT_FILE $whichOutput;
close OUT_FILE;
qx`gvim -geometry 100x50 $myoutput `;
}
##########################################
# END OF FILE
##########################################



Make an alias:
> alias vich vich.pl

Now instead of doing:

> which myscript
/usr/bin/myscript
> vim /usr/bin/myscript

you can just do:

> vich myscript

Just a little thing to speed things up..

Make your config files very flexible (w/ Perl eval)

I have to make my config files flexible so that I can allow my users a lot of options and even override functions. I can do this by making my config file "eval"-able. Meaning that the config file is actually Perl code that can be interpreted by the calling Perl script.

Most of the code was taken from:

http://www.usenix.org/publications/perl/perl13.html

Here is my proof of concept code:

##### FILE: evalConfigExample.pl #####


#!/usr/bin/perl -w
# Filename: evalConfigFileExample.pl
# This is a proof of concept script to show how one can use a perl script as a config.
# file. This allows much more freedom when declaring a configuration file. One can
# call functions, redefine functions and generally do anything that you could.
# normally do in a regular perl script. Ex:
#; ~/bin/evalConfigFileExample.pl -file ~/bin/evalConfigFileExample.cfg

use Getopt::Long;
use Data::Dumper;
use Pod::Usage;

my $page_info ;
my %opts = ();
my $ok = GetOptions(\%opts,
'file=s',
'help'
);
if ( !$ok ) { die; }
pod2usage(1) if (exists ( $opts{help} ) );

sub printer {
print "In Perl prog\n";
}

sub hello_world {
print "Calling hello_world from eval'd config file!\n";
}

sub parse_cfg {
my($file) = @_;
delete($INC{$file});
eval('require("$file")');
die "*** Failed to eval() file $file:\n$@\n" if ($@);
print ("VENDOR = $VENDOR \n");
}

printer();
parse_cfg( $opts{file} );
print ("VENDOR = $VENDOR \n");
printer();

##########################################
# END OF FILE
##########################################



##### FILE: evalConfigExample.cfg #####


#evalConfigFileExample.cfg
$VENDOR = "Sun";
$HARDWARE = "Sparc";
$OS = "Solaris";
$VERSION = "2.5";
$HOSTNAME = `/bin/hostname`;
$PSCMD = "/usr/bin/ps -ef";
hello_world();

sub printer {
print "In required file\n";
}

##########################################
# END OF FILE
##########################################



The output should look like this:


> evalConfigFileExample.pl -file evalConfigFileExample.cfg
In Perl prog
Subroutine printer redefined at evalConfigFileExample.cfg line 9.
Calling hello_world from eval'd config file!
VENDOR = Sun
VENDOR = Sun
In required file

Sunday, February 1, 2009

Sunday, October 14, 2007

Vimperator -- vimified browsing (at last)

I have found the coolest productivity tool since vim. Vimperator! This allows you to do modal browsing much in the same way that vim does. The greatest thing is that it allows you to write maps (keybindings) that can dramatically improve your productivity on the web. What is so great about modal editing and browsing? You don't have to use that pesky mouse as much anymore. Your fingers stay near home row more often and allow you to control almost everything from they keyboard.

Here is a link from linux.com that has a somewhat decent discussion about it. I am sure there will be many more articles written bout this great tool. Here is the offical wiki.


It is only at version 0.52 as of this writing so I am sure there are improvements to come but it already has vastly sped up my surfing. One vim like feature I would like to see is the visual mode for selecting things. Your mappings and customizations (like vimrc file) are stored in a file named vimperator. Here is what my vimperatorrc file looks like so far:


set activate=quickmark,paste
set nobeep
set complete=sbfh
set defsearch=google
set guioptions=
set maxhints=1000
set preload
set previewheight=20
set showmode
set showstatuslinks
set showtabline=2
set titlestring=Vimperator Dev
set verbose=9
set wildmode=list:full

" start MozRepl
js < :restart

" select next/previous tab
map gt
map gT
map ,src :source ~/.vimperatorrc

" move current tab left/right
map :tabmove! +1
map :tabmove! -1

" move up and down one page
map
map

" Tab maps
map :tabopen
map :tabnext
map :tabprevious

" backward and forward
map gk :back
map gj :forward

map :quit

echo .vimperatorrc sourced

" vim: ft=vimperator sw=2 sts=2

Thursday, July 13, 2006

New Job

I gave my resignation to Sigmatel on Monday. I will be doing the same kind of work (pre-silicon verification) but as a contractor/consultant at Analog Devices here in Austin. They are a pretty good company and they are starting to use some of the newer verification methodologies so I will be helping them bring this up. Should be very exciting. The world of verification is getting very exciting!

Read more at blog.360.yahoo.com/blog...

Friday, June 23, 2006

CNN.com - Study: Earth 'likely' hottest in 2,000 years - Jun 22, 2006

First to add a comment to this news. I just wanted to test to see how adding a new comment to a page would look. Hopefully this will motivate people to do something about global warming.

Read more at www.cnn.com/2006/TECH/s...