简体   繁体   English

提取两个模式(不包括模式)之间的文本

[英]Extract text between two patterns excluding patterns

Consider the output of java -jar plantuml.jar -language : 考虑一下java -jar plantuml.jar -language的输出:

;type
;26
abstract
actor

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

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

;EOF

I need to extract colors from this text without surrounding strings. 我需要从此文本中提取颜色而不包含字符串。 I have read several articles and Q&As and did not found answer. 我已经阅读了几篇文章和问答,但没有找到答案。 Here i found the most suitable answer. 在这里,我找到了最合适的答案。

$ 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

There is a small nuance: ;147 could be any other value and EOF could be changed at any time to something other. 有一个细微差别: ;147可以是任何其他值,并且EOF可以随时更改为其他值。 I tried sed -n '/\\;color\\s*\\;\\d*/,/\\n\\n/ , but it returns nothing. 我试过sed -n '/\\;color\\s*\\;\\d*/,/\\n\\n/ ,但是什么也没返回。 Please help me to achieve the next result: 请帮助我获得下一个结果:

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'

With sed to delete all lines between patterns not starting with ; 用sed删除模式之间所有不以开头的行; :

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

To delete last blank line: 要删除最后一个空白行:

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

or with GNU sed: 或使用GNU sed:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM