seek.pl



NAME

seek.pl <pattern> <glob> <editor>


DESCRIPTION

Seek recursively for <pattern> in the <glob> files and edit found lines in <editor>.


COPYRIGHT

Copyright 2010 Etienne LEMEE <coding AT etilem DOT net>

This piece of code is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


#!/usr/bin/perl -w

use strict;

@ARGV > 2 or die << "USAGE";

usage: $0 <pattern> <glob> <editor>

seek recursively for <pattern> in the <glob> files and edit found lines in <editor>
USAGE

my ($pat, $glo, $tmp) = @ARGV; 
my $edi = qx! which $tmp ! or die "Can't find '$tmp' on your system !";
chomp $edi;

for my $file (qx! find -type f -name '$glo' !) { # Yes, I've been told about File::Find ;)
    chomp $file;
    open my $fh, '<', $file;
    while (<$fh>) { 
        system "$edi +$. $file" if (/$pat/);
    }
    close $fh;
}