2017-09-26 58 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。我能做到這一點在Perl,但我有一個問題,因爲我不知道怎麼行至開始/

我的腳本如下:

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; 

我得到一個錯誤:

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/