简体   繁体   中英

Extract text between two patterns excluding patterns

Consider the output of 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. I tried sed -n '/\\;color\\s*\\;\\d*/,/\\n\\n/ , but it returns nothing. 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 -n '/^;color/,/^;EOF/{/;/d;p}' file

To delete last blank line:

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

or with GNU sed:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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