Reputation: 935
okay, i am not sure whether this is even possible or not..I may sound stochastic... There are around 250 files with names as Eg:1
1_0.pdb,1_60.pdb,1_240.pdb,....50_0.pdb,50_60.pdb,50_240.pdb.....
having some data.
Now for each of the above file there is a another file of same name....just prefix file is added...Like: E.g:2
file1_0.pdb,file1_60.pdb,file1_240.pdb,....file50_0.pdb,file50_60.pdb,file50_240.pdb.....
again having some data.
is there a code possible that can copy data from each file from first example and paste it to its corresponding file in example2..? like from 1_0.pdb to file1_0.pdb...I hope iam not random and more clear...
Upvotes: 1
Views: 480
Reputation: 7579
With perl you could do something like
#!/usr/bin/perl -w
use strict;
my @filenames = qw(1_0.pdb 1_60.pdb 1_240.pdb);
for my $filename (@filenames) {
open(my $fr, '<', $filename) or next;
open(my $fw, '>>', "file$filename") or next;
local($/) = undef;
my $content = <$fr>;
print $fw $content;
close $fr;
close $fw;
}
EDIT:
Instead of listing all filnames in
my @filenames = qw(1_0.pdb 1_60.pdb 1_240.pdb);
you could do something like
my @filenames = grep {/^\d+_\d+/} glob "*.pdb";
Upvotes: 3
Reputation: 2247
This shell script will also work
foreach my_orig_file ( `ls *.pdb | grep -v ^file` )
set my_new_file = "file$my_orig_file"
cat $my_orig_file >> $my_new_file
end
Upvotes: 0
Reputation: 1296
Give this code a try:
use strict;
use warnings;
foreach my $file (glob "*.pdb") {
next if ($file =~ /^file/);
local $/ = undef;
my $newfile = "file$file";
open(my $fh1, "<", $file) or die "Could not open $file: " . $!;
open(my $fh2, ">>", $newfile) or die "Could not open $newfile: " . $!;
my $contents = <$fh1>;
print $fh2 $contents;
close($fh1);
close($fh2);
}
If you want to overwrite the contents of the files rather than appending, change ">>"
to ">"
in the second open
statement.
Upvotes: 1