greatshots has asked for the wisdom of the Perl Monks concerning the following question:

monks,

<metaattribute metaattribute-name="property" metaobject-name="sys +tem" object-link=""> . . . <metaattribute metaattribute-name="nc_id" metaobject-name="Card" objec +t-link="">
from the input lines I would like to match the lines 'metaattribute metaattribute-name' and want the output similar to the below
system.property Card.nc_id . . . <metaobject-name>.<metaattribute-name>
how can I do this from commandline using perl ?

Replies are listed 'Best First'.
Re: one line regular expression
by Corion (Patriarch) on Oct 17, 2006 at 07:34 UTC

    Parsing tagged data with regular expressions is always error-prone, but if you know that your data is not too crazy, you can get pretty far with it:

    perl -lne 'print qq($2.$1) if /<metaatribute metaattribute-name="([^"] +*?)" metaobject-name="([^"]*?"/'

    If you want to run this on Win32, you have to fiddle around with the double and single quotes. I'd try the following then:

    perl -lne "$q = chr(34); print qq($2.$1) if /<metaatribute metaattribu +te-name=$q([^$q]*?)$q metaobject-name=$q([^$q]*?$q/"

    Update: shmem spotted a typo in my print statements

Re: one line regular expression
by shmem (Chancellor) on Oct 17, 2006 at 07:36 UTC
    What have you tried so far? It can be as simple as
    perl -nle '/metaattribute metaattribute-name="([^"]+)" metaobject-name +="([^"]+)"/ and print "$2.$1"' textfile
    if the attributes are always in order.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}