簡體   English   中英

搜索特定的字符串並在特定的字符串之間打印-Linux Shell腳本

[英]Search for particular string and print between particular strings — Linux Shell Script

這是我的檔案。

'#### 
OutOfmemory is the error which has occured
Log Error
Sample
'####
Incident Dump Executor 
Test
Notinhg
'####
Sample
test
'####
OutOfmemory Sample
This is what i want
'####

從這個文件中,我想找到OutOfmemory,一旦找到,我需要打印####之間的行

OutOfmemory發生兩次,所以我想要這樣的輸出:

'#### 
OutOfmemory is the error which has occured
Log Error
Sample
'####

'####
OutOfmemory Sample
This is what i want
'####

我不想在輸出中剩余行。

grep-A (之后)和-B (之前)選項一起使用,可以指定找到匹配之后/之前顯示多少行。 假設您的文件名為foo ,您可以這樣做:

cat foo | grep -i -B 1 -A 2 "OutOfmemory"

使用gnu-awk可以做到這一點:

awk -v RS="'####" 'BEGIN{ORS=RS "\n\n"} /OutOfmemory/{print RT $0}' file

'####
OutOfmemory is the error which has occured
Log Error
Sample
'####

'####
OutOfmemory Sample
This is what i want
'####

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM