[Project_owners] AWK related question

Atte Kemppilä atte.kemppila at iki.fi
Thu Aug 30 06:42:37 PDT 2007


On Wed August 29 2007 16:33, Michael Vincent van Rantwijk wrote:
> Atte Kemppilä wrote:
> > On Wed August 29 2007 08:54, Michael Vincent van Rantwijk wrote:
> >> I can do nice things with AWK these days, but the one thing I still
> >> don't get is how I should print the matching data of a search/regex like
> >> for example: /foobar/ { print } as in how do I get it to dump "foobar"
> >> (where foobar can be anything I need)?
> >
> > Not quite sure that I understood the question but do you mean this:
>
> Let's take the following example:
>
> <description>Some Random Text</description>
>
> should become:
>
> <!ENTITY .description "Some Random Text">
>
> Note: I want to do something similar for:
>
> label="A Random Label Attribute"
> accesskey="A Random Accesskey Attribute"
> label value="A Random Value Attribute"
>
> Which should be come:
>
> <!ENTITY .label "A Random Label Attribute">
> <!ENTITY .accesskey "A Random Accesskey Attribute">
> <!ENTITY .value "A Random Value Attribute">

$ gawk --version
GNU Awk 3.1.5

$ echo '<label>text</label>' | gawk 'match($0, /<(description|label|accesskey|
value)>([^<]*)<\/[^>]*>/, arr) { print "<!ENTITY ." arr[1] " \"" arr[2] 
"\">" }'

<!ENTITY .label "text">


Tag matching is kind of simplistic in the above. You could make it better 
using ranges. But then again, why are you using awk with xml data. Wouldn't 
xsl be The Right Tool for this.

-- 
Atte Kemppilä


More information about the Project_owners mailing list