2013-10-24 44 views
1

我有一個文本文件以下(input.txt中):如何刪除用Perl的正則表達式短語之前的所有內容(包括新行)

/dsdjf48 84f3jh0-=-=-+F_+_m a= 
^^^^^^^^^ 
Random Words 
vvvvvvvvv 
fkls;[]saf\as-1f-d=fd-s=dsf=-s 

Hello, now I will start typing a sentence. 

我需要你好,現在我將之前」清除一切「用perl正則表達式,我相信支持這個。 下面是我用命令:

perl -p -e "s/search-term/replace-term/" input.txt 

當我進行任何正則表達式搜索,它會搜索每行線。有沒有一個標誌來啓用多行搜索?

回答

1
perl -n -e 'print if $_ =~ /^Hello, now I will start typing a sentence\.$/' input.txt 
2

這應該這樣做,使用二進制..這裏的flip-flop操作。

perl -pe '$_ = "" unless /^Hello, now/ .. 1' input.txt 

使用-i(內聯選項)可以將更改保存在輸入文件本身中。

perl -pi -e '$_ = "" unless /^Hello, now/ .. 1' input.txt 

在這種情況下也可能使用=!!

perl -pi -e '$_ x=!! (/^Hello, now/ .. 1)' input.txt 
+0

'走線。 。1'看起來不錯 –

+0

當第二個布爾值爲真時,'$ b'以'E0'結尾。檢查http://perldoc.perl.org/perlop.html#Range-Operators –

+0

好的,這工作。只是想知道,你好,現在字段仍然遵循普通的Perl正則表達式? –

1

你可以通過改變輸入記錄分隔符來undef一次發出聲音整個文件,

perl -0777 -pe 's/.*? (?=Hello)//xs' input.txt 

,但更好的方法是通過線

perl -ne '$b = 1 .. /^Hello/; print if $b =~ /^$|E0$/' input.txt 
1
> perl -i -lne 'BEGIN{$f=0}if(/Hello/){$f=1}if($f){print}' your_file 
相關問題