2

I have a input text like below:

 10779: (255,255,  0,255) #FFFF00 yellow
   338: (255,238,125,255) #FFEE00 srgba(255,238,125,1)
   180: (238,221,  0,255) #EEDD00 srgba(238,221,0,1)
 ....

I wish the output is

 255,255,0,255    
 255,238,125,255    
 238,211,0,255

How to extract this data from the input using gawk/awk? I've tried this:

gawk 'BEGIN {FS=\"[:,()]\"};{print $3,$4,$5,$6}'

but this gives me space between the data, e.g. 255 255 0 255

realmq
  • 429
  • 4
  • 18
  • You might find it easier to understand how to do this using regular expressions in a language like Perl, Python, Ruby, etc. Are you open to a solution in a language other than awk? – John Zwinck Apr 05 '14 at 03:37
  • Set `OFS=","` so that fields are separated by commas on output. – Jonathan Leffler Apr 05 '14 at 03:46

4 Answers4

3

Based on your input I am guessing this might work

awk -F'[()]' '{gsub(/ /,"",$2); print $2}' file
255,255,0,255
255,238,125,255
238,221,0,255
iruvar
  • 22,736
  • 7
  • 53
  • 82
1

Using perl:

perl -lne 'print $1 if(/\((.*?)\)/)'

If you donot want those spaces, then:

perl -lne '$a=$1 if(/\((.*?)\)/);$a=~s/ //g;print $a'

Awk:

awk '{a=index($0,"(");b=index($0,")");a++;b--;print substr($0,a,b-a)}'

Tested

Vijay
  • 65,327
  • 90
  • 227
  • 319
1

Another way with awk:

$ awk -F'[(#]' 'gsub(/[ )]/,"")&&$0=$2' file
255,255,0,255
255,238,125,255
238,221,0,255
jaypal singh
  • 74,723
  • 23
  • 102
  • 147
1

A sed solution, for variety:

sed -E 's/^.+\(([^)]+)\).+$/\1/; s/ //g' file

Or, one step at a time, with cut and tr:

cut -d '(' -f 2 file | cut -d ')' -f 1 | tr -d ' '
mklement0
  • 382,024
  • 64
  • 607
  • 775