2017-09-26 4 views
2

私はそうのように見えますlist.txtというファイルがあります(それは500以上の行を持つ)たPerl:で始まる行を指定するためにどのように「/」

/apps/gtool/0.7.5/gtool -M --g gen1.txt etc 
/apps/gtool/0.7.5/gtool -M --g gen2.txt etc 
/apps/gtool/0.7.5/gtool -M --g gen3.txt etc 

私はそれぞれの行と.SHスクリプトを作りたいですlist.txt。私はエラーを取得する

use strict; 
use warnings; 

open (IN, "<list_for_merging_chunks.sh"); 
while (<IN>) 
{ 
    if ($_=~ m/^/apps.*\n/) 
    { 
    my $file = $_; 
    $file =~ s/.*\> //; 
    $file =~ s/\.txt/.sh/; 
    $file =~ s/\n//; 
    open (OUT, ">$file"); 
    print OUT "\#!/bin/bash\n\#BSUB -J \"$file\"\n\#BSUB -o 
/scratch/home/\n\#BSUB -e /scratch/home/$file\.out\n#BSUB -n 1\n\#BSUB -q 
normal\n\#BSUB -P DBCDOBZAK\n\#BSUB -W 168:00\n"; 
    print OUT $_; 
    close OUT; 
    } 

} 

exit; 

:私はPerlでこれを行うことができますが、私は次のように私のスクリプトがどのように指名ラインが/

で始まるかわからないように私はこの問題を持っている

Bareword found where operator expected at merging_chunks.pl line 7, near "*\n" 
    (Missing operator before n?) 
"my" variable $file masks earlier declaration in same statement at 
merging_chunks.pl line 10. 
"my" variable $file masks earlier declaration in same scope at 
merging_chunks.pl line 11. 
"my" variable $file masks earlier declaration in same scope at 
merging_chunks.pl line 12. 
"my" variable $file masks earlier declaration in same scope at 
merging_chunks.pl line 14. 
"my" variable $file masks earlier declaration in same scope at 
merging_chunks.pl line 15. 
"my" variable $file masks earlier declaration in same statement at 
merging_chunks.pl line 15. 
"my" variable $file masks earlier declaration in same statement at 
merging_chunks.pl line 15. 
"my" variable $_ masks earlier declaration in same scope at merging_chunks.pl 
line 16. 
syntax error at merging_chunks.pl line 7, near "*\n" 
syntax error at merging_chunks.pl line 20, near "}" 
Execution of merging_chunks.pl aborted due to compilation errors. 

私がこのファイルをどうするだと思う:if ($_=~ m/^/apps.*\n/) それはそれは/で始まるという事実を好きにいないようです。とにかく私はこれを回避することができますか?私は何とかPerlに伝えるために使うことができる特殊文字があると仮定していますか?どうもありがとう。

答えて

2

あなたはblackslashで正規表現でメタ文字をエスケープすることができます。

m/^\/apps.*\n/ 

このようにパターンマッチの区切り文字を変更することもできます。

m{^/apps.*\n} 

あなたのコードでは、二重引用符で囲まれた文字列の中で既に行っているように、あなたは知っているようです。

$_で操作する場合は、$_ =~の部分は必要ありません。 m//を使用している場合は、$_であることが暗示されています。

1

regexpr区切り文字を、regexpr内で使用されていない文字で変更します。この例では、私は!の代わり/を使用します。

$_=~ m!^/apps.*\n! 

または/文字をスケープ:

$_ =~ m/^\/apps.*\n/ 
関連する問題