[英]copying data from multiple files and adding to different files
好吧,我不確定這是否可能。我可能聽起來是隨機的......有大約250個文件,其名稱為Eg:1
1_0.pdb,1_60.pdb,1_240.pdb,....50_0.pdb,50_60.pdb,50_240.pdb.....
有一些數據。
現在對於上面的每個文件都有一個同名的另一個文件....只添加前綴文件...喜歡:例如:2
file1_0.pdb,file1_60.pdb,file1_240.pdb,....file50_0.pdb,file50_60.pdb,file50_240.pdb.....
再次有一些數據。
是否有可能的代碼可以從第一個示例中復制每個文件中的數據並將其粘貼到example2中的相應文件中。? 比如從1_0.pdb到file1_0.pdb ...我希望iam不隨機,更清晰......
使用perl你可以做類似的事情
#!/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;
}
編輯:
而不是列出所有的電影名稱
my @filenames = qw(1_0.pdb 1_60.pdb 1_240.pdb);
你可以做點什么
my @filenames = grep {/^\d+_\d+/} glob "*.pdb";
嘗試使用此代碼:
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);
}
如果要覆蓋文件的內容而不是附加,請在第二個open
語句中將">>"
更改為">"
。
這個shell腳本也可以工作
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.