繁体   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