簡體   English   中英

提取兩個模式(不包括模式)之間的文本

[英]Extract text between two patterns excluding patterns

考慮一下java -jar plantuml.jar -language的輸出:

;type
;26
abstract
actor

............................

;color
;147
AliceBlue
AntiqueWhite
Aqua
Aquamarine
............................
Wheat
White
WhiteSmoke
Yellow
YellowGreen

;EOF

我需要從此文本中提取顏色而不包含字符串。 我已經閱讀了幾篇文章和問答,但沒有找到答案。 在這里,我找到了最合適的答案。

$ java -jar plantuml.jar -language | sed -n '/\;color/,/\n\n/{/color/!{/\n\n/!p}}'
;147
AliceBlue
AntiqueWhite
Aqua
Aquamarine
Azure
Beige
Bisque
....................
Teal
Thistle
Tomato
Turquoise
Violet
Wheat
White
WhiteSmoke
Yellow
YellowGreen

;EOF

有一個細微差別: ;147可以是任何其他值,並且EOF可以隨時更改為其他值。 我試過sed -n '/\\;color\\s*\\;\\d*/,/\\n\\n/ ,但是什么也沒返回。 請幫助我獲得下一個結果:

AliceBlue
AntiqueWhite
Aqua
Aquamarine
Azure
Beige
Bisque
....................
Teal
Thistle
Tomato
Turquoise
Violet
Wheat
White
WhiteSmoke
Yellow
YellowGreen

它聽起來像您需要的是:

awk '/^;/{if (/[[:alpha:]]/) f=(/color/?1:0); next} f'

用sed刪除模式之間所有不以開頭的行;

sed -n '/^;color/,/^;EOF/{/;/d;p}' file

要刪除最后一個空白行:

sed -n '/^;color/,/^;EOF/{/;/d;/^$/d;p}' file

或使用GNU sed:

sed -n '/^;color/,/^;EOF/{/^;\|^$/d;p}' file

暫無
暫無

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

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