我正在学习 Perl,我正在尝试弄清楚如何完成这项任务。我有一个包含一堆文本文件的文件夹,我有一个ions_solvents_cofactors
包含三个字母列表的文件。
我编写了一个脚本,该脚本打开并读取文件夹中的每个文件,并应删除特定列 [3] 下与列表中的某些元素匹配的那些行。它工作不正常。我在脚本末尾遇到了一些问题,无法弄清楚它是什么。
我得到的错误是:rm: invalid option -- '5'
我的输入文件如下所示:
ATOM 1592 HD13 LEU D 46 11.698 -10.914 2.183 1.00 0.00 H
ATOM 1593 HD21 LEU D 46 11.528 -8.800 5.301 1.00 0.00 H
ATOM 1594 HD22 LEU D 46 12.997 -9.452 4.535 1.00 0.00 H
ATOM 1595 HD23 LEU D 46 11.722 -8.718 3.534 1.00 0.00 H
HETATM 1597 N1 308 A 1 0.339 6.314 -9.091 1.00 0.00 N
HETATM 1598 C10 308 A 1 -0.195 5.226 -8.241 1.00 0.00 C
HETATM 1599 C7 308 A 1 -0.991 4.254 -9.133 1.00 0.00 C
HETATM 1600 C1 308 A 1 -1.468 3.053 -8.292 1.00 0.00 C
这是脚本:
#!/usr/bin/perl -w
$dirname = '.';
opendir( DIR, $dirname ) or die "cannot open directory";
@files = grep( /\.txt$/, readdir( DIR ) );
foreach $files ( @files ) {
open( FH, $files ) or die "could not open $files\n";
@file_each = <FH>;
close FH;
close DIR;
my @ion_names = ();
my $ionfile = 'ions_solvents_cofactors';
open( ION, $ionfile ) or die "Could not open $ionfile, $!";
my @ion = <ION>;
close ION;
for ( my $line = 0; $line <= $#file_each; $line++ ) {
chomp( $file_each[$line] );
if ( $file_each[$line] =~ /^HETATM/ ) {
@is = split '\s+', $file_each[$line];
chomp $is[3];
}
foreach ( $file_each[$line] ) { #line 39
if ( "@ion" =~ $is[3] ) {
system( "rm $file_each[$line]" );
}
}
}
}
因此,例如,如果308
从输入文件中匹配文件 ions_cofactors_solvents`,则删除所有匹配的行。