sed mogelijkheid erweer uit gehaald. dit is Perl gvd. ;)

-p pattern werkt nu
This commit is contained in:
Ward Wouts 2001-08-01 08:50:18 +00:00
parent 053bdc1b09
commit 7087a82261

View file

@ -12,7 +12,7 @@
use IO::File; use IO::File;
use File::Copy; use File::Copy;
use POSIX qw(tmpnam); use POSIX qw(tmpnam);
use Getopt::Std; use Getopt::Long;
&cmdline; &cmdline;
@ -26,33 +26,21 @@ closedir(DIRHANDLE);
&lock; &lock;
# make tempfiles and install handler to remove them if (@pattern) {
do { $target_name = tmpnam() } @target = @source;
until $target = IO::File->new($target_name, O_RDWR|O_CREAT|O_EXCL); &pattern_edit;
END { unlink($target_name) or die "Couldn't unlink $target_name: $!";
unlink(".mv_wrap") or die "Couldn't unlink .mv_wrap: $!"; }
foreach (@source) {
print $target $_;
}
close ($target);
if ($option{s}) {
&stream_edit($target_name);
} else { } else {
&edit($target_name); $temp_file = &open_temp;
&edit($temp_file);
&read_temp($temp_file);
} }
open $target, $target_name or die "Couldn't open tempfile: $target_name: $!";
@target = <$target>;
close ($target);
# run checks
unless ( scalar(@source) == scalar(@target) ) { unless ( scalar(@source) == scalar(@target) ) {
die "Aborting. Source and target list don't have the same number of lines.\n"; die "Aborting. Source and target list don't have the same number of lines.\n";
} }
# ook nog checken op lege regels hier....
foreach $line (@target) { foreach $line (@target) {
if ( $line =~ m/^$/ ) { die "Aborting. You can't move to empy names.\n"; } if ( $line =~ m/^$/ ) { die "Aborting. You can't move to empy names.\n"; }
} }
@ -70,36 +58,35 @@ if (@unsafe = &check_safety(\@source, \@target)) {
# final move # final move
&move_files(\@source, \@target); &move_files(\@source, \@target);
########################################################
# call EDITOR or vi with filename # call EDITOR or vi with filename
# edit($filename); # edit($filename);
sub edit { sub edit {
my $filename = shift; my $filename = shift;
@editor = (defined $ENV{EDITOR} ? $ENV{EDITOR} : "vi", "$filename"); @editor = (defined $ENV{EDITOR} ? $ENV{EDITOR} : "vi", "$filename");
if ($option{e}) { @editor = ($option{e}, "$filename") } if ($opt_e) { @editor = ($opt_e, "$filename") }
system(@editor) == 0 system(@editor) == 0
or die "System @editor failed: $?"; or die "System @editor failed: $?";
} }
sub stream_edit { # make tempfiles and install handler to remove them
my $filename = shift; sub open_temp {
my ($pid, @output); my $target;
@editor = (split(" ", $option{s}), "$filename"); do { $target_name = tmpnam() }
# foreach (@editor) { print "$_\n"; } until $target = IO::File->new($target_name, O_RDWR|O_CREAT|O_EXCL);
die "cannot fork: $!" unless defined ($pid = open(KID, "-|")); END { if ($opt_e) { unlink($target_name) or die "Couldn't unlink $target_name: $!";} }
if ($pid) { #parent foreach (@source) {
while (<KID>) { print $target $_;
# print ;
push @output, $_;
}
close(KID);
} else { #child
exec(@editor) or die "can't exec program: $!\n";
} }
open OUT, ">$filename" or die "Can't open $filename for writeing: $!\n"; close ($target);
foreach $line (@output) { return $target_name;
print OUT "$line"; }
}
close OUT; sub read_temp {
open $target, $target_name or die "Couldn't open tempfile: $target_name: $!";
@target = <$target>;
close ($target);
} }
# moves files from the names in one array to the names in another array # moves files from the names in one array to the names in another array
@ -120,6 +107,21 @@ sub move_files {
} }
} }
sub pattern_edit {
my (@new_target, $pat);
foreach (@target) {print ;}
foreach $pat (@pattern) {
@new_target=();
foreach(@target) {
eval $pat;
push @new_target, $_;
}
@target = @new_target;
foreach (@target) {print;}
}
}
# returns 0 if all entries in @target_list are unique. 1 if not. # returns 0 if all entries in @target_list are unique. 1 if not.
sub check_unique (@target_list){ sub check_unique (@target_list){
my @uniqu; my @uniqu;
@ -183,11 +185,17 @@ sub lock {
} }
} }
sub cmdline { sub unlock {
%option = (); unlink(".mv_wrap") or die "Couldn't remove lock file: $!\n";
getopts("e:hs:", \%option); }
&help if $option{h}; sub cmdline {
%optctl = ();
# getopts("e:hp:", \%option);
&GetOptions("e=s", "h", "p=s", \@pattern);
&GetOptions(\%optctl, "e");
&help if $opt_h;
} }
sub help { sub help {
@ -201,10 +209,8 @@ environment variable. Which in turn can be overridden with the -e option.
At the moment it takes the following options: At the moment it takes the following options:
-h this help message -h this help message
-e "editor" invoke with this editor -e "editor" invoke with this editor
-s "streameditor" use a stream editor -p "pattern" use a pattern to edit
this makes things like: -f "file" use a pattern file to edit
mv_wrap.pl -s "sed -e s/1/5/"
possible
EOT EOT
exit; exit;