sed mogelijkheid erweer uit gehaald. dit is Perl gvd. ;)
-p pattern werkt nu
This commit is contained in:
parent
053bdc1b09
commit
7087a82261
1 changed files with 54 additions and 48 deletions
100
mvwrap/mvwrap
100
mvwrap/mvwrap
|
|
@ -12,7 +12,7 @@
|
|||
use IO::File;
|
||||
use File::Copy;
|
||||
use POSIX qw(tmpnam);
|
||||
use Getopt::Std;
|
||||
use Getopt::Long;
|
||||
|
||||
&cmdline;
|
||||
|
||||
|
|
@ -26,33 +26,21 @@ closedir(DIRHANDLE);
|
|||
|
||||
&lock;
|
||||
|
||||
# make tempfiles and install handler to remove them
|
||||
do { $target_name = tmpnam() }
|
||||
until $target = IO::File->new($target_name, O_RDWR|O_CREAT|O_EXCL);
|
||||
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);
|
||||
if (@pattern) {
|
||||
@target = @source;
|
||||
&pattern_edit;
|
||||
} 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) ) {
|
||||
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) {
|
||||
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
|
||||
&move_files(\@source, \@target);
|
||||
|
||||
########################################################
|
||||
|
||||
# call EDITOR or vi with filename
|
||||
# edit($filename);
|
||||
sub edit {
|
||||
my $filename = shift;
|
||||
@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
|
||||
or die "System @editor failed: $?";
|
||||
}
|
||||
|
||||
sub stream_edit {
|
||||
my $filename = shift;
|
||||
my ($pid, @output);
|
||||
@editor = (split(" ", $option{s}), "$filename");
|
||||
# foreach (@editor) { print "$_\n"; }
|
||||
die "cannot fork: $!" unless defined ($pid = open(KID, "-|"));
|
||||
if ($pid) { #parent
|
||||
while (<KID>) {
|
||||
# print ;
|
||||
push @output, $_;
|
||||
# make tempfiles and install handler to remove them
|
||||
sub open_temp {
|
||||
my $target;
|
||||
do { $target_name = tmpnam() }
|
||||
until $target = IO::File->new($target_name, O_RDWR|O_CREAT|O_EXCL);
|
||||
END { if ($opt_e) { unlink($target_name) or die "Couldn't unlink $target_name: $!";} }
|
||||
foreach (@source) {
|
||||
print $target $_;
|
||||
}
|
||||
close(KID);
|
||||
} else { #child
|
||||
exec(@editor) or die "can't exec program: $!\n";
|
||||
close ($target);
|
||||
return $target_name;
|
||||
}
|
||||
open OUT, ">$filename" or die "Can't open $filename for writeing: $!\n";
|
||||
foreach $line (@output) {
|
||||
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
|
||||
|
|
@ -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.
|
||||
sub check_unique (@target_list){
|
||||
my @uniqu;
|
||||
|
|
@ -183,11 +185,17 @@ sub lock {
|
|||
}
|
||||
}
|
||||
|
||||
sub cmdline {
|
||||
%option = ();
|
||||
getopts("e:hs:", \%option);
|
||||
sub unlock {
|
||||
unlink(".mv_wrap") or die "Couldn't remove lock file: $!\n";
|
||||
}
|
||||
|
||||
&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 {
|
||||
|
|
@ -201,10 +209,8 @@ environment variable. Which in turn can be overridden with the -e option.
|
|||
At the moment it takes the following options:
|
||||
-h this help message
|
||||
-e "editor" invoke with this editor
|
||||
-s "streameditor" use a stream editor
|
||||
this makes things like:
|
||||
mv_wrap.pl -s "sed -e s/1/5/"
|
||||
possible
|
||||
-p "pattern" use a pattern to edit
|
||||
-f "file" use a pattern file to edit
|
||||
|
||||
EOT
|
||||
exit;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue