0

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
Community
  • 1
  • 1
kyb
  • 7,233
  • 5
  • 52
  • 105
  • Are those `....` lines REALLY present in your input/output? If not then get rid of them from your question. Make sure your sample input/output is something we could run a potential solution against to test it. – Ed Morton Mar 09 '17 at 18:45

2 Answers2

2

It SOUNDS like all you need is:

awk '/^;/{if (/[[:alpha:]]/) f=(/color/?1:0); next} f'
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • 1
    nice! thank you. Can you please explain in details how does it work. – kyb Mar 09 '17 at 18:50
  • 1) What is `?1:0`, 2) what is `f`? – kyb Mar 09 '17 at 18:52
  • 1) `(/color/?1:0)` is a ternary expression (see https://en.wikipedia.org/wiki/Ternary_operation). 2) `f` is a flag variable using the common abbreviation of `f`=`found`, it gets set when "color" is on the `;`-line and cleared when some other alphabetic value is on the `;`-line. – Ed Morton Mar 09 '17 at 18:56
  • How to trim trailing line feed? – kyb Mar 09 '17 at 19:16
  • 1
    idk what that means but if you're asking how to only print non-blank lines then change the `f` at the end to `f && NF`. – Ed Morton Mar 09 '17 at 19:19
1

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
SLePort
  • 15,211
  • 3
  • 34
  • 44