2017-08-07 157 views
-2

輸入文件:如何使用perl腳本或命令行刪除與特定模式匹配的文件中的幾行?

### 
Hi 
I am Jack 
I live in London 
#### 
I am James 
I live in Germany 
#### 

在這裏,我想搜索「傑克」,並刪除散列之間的一切。

預期輸出:

### 
I am James 
I live in Germany 
#### 
+0

@PerlDuck輸入以破碎的方式顯示。這是行不通的。 – simbabque

+3

你有沒有嘗試過自己的東西?你在哪裏掙扎? – simbabque

+0

@simbabque你爲什麼認爲它被打破了?你現在做了三條線,我們以前只有一條線。您將其更改爲其他問題。 – PerlDuck

回答

3

祕訣是使用$/定義「塊」,你所期待的。

#!/usr/bin/perl 

use strict; 
use warnings; 

local $/ = '###'; 

while (<DATA>) { 
    print unless /Jack/; 
} 

__DATA__ 
### 
Hi 
I am Jack 
I live in London 
#### 
I am James 
I live in Germany 
#### 
相關問題