Answer the question
In order to leave comments, you need to log in
Awk parsing xml?
Hey!
Given:
1. xml file
<font color="black"><font color="#0000ff"><?</font><font color="#800000">xml</font> <font color="#ff0000">version</font>=<font color="#ff0000">1</font>.<font color="#ff0000">0</font>?<font color="#0000ff">></font><br/> <font color="#0000ff"><</font><font color="#800000">file_events</font><font color="#0000ff">></font><br/> <font color="#0000ff"><</font><font color="#800000">event</font> <font color="#ff0000">date</font><font color="#0000ff">="1254728164000"</font> <font color="#ff0000">author</font><font color="#0000ff">="Bin/.svn/entries"</font> <font color="#ff0000">filename</font><font color="#0000ff">="f4d64c1a/497b733f81c2866d/81c2866da7e4d268.68"</font> <font color="#ff0000">action</font><font color="#0000ff">="D"</font> <font color="#ff0000">comment</font><font color="#0000ff">=""</font><font color="#0000ff">/></font><br/> <font color="#0000ff"><</font><font color="#800000">event</font> <font color="#ff0000">date</font><font color="#0000ff">="1254728164000"</font> <font color="#ff0000">author</font><font color="#0000ff">="Bin/.svn/entries"</font> <font color="#ff0000">filename</font><font color="#0000ff">="51d46ff1/fdb0cf112ec24d1e/2ec24d1e87c7a87a.7a"</font> <font color="#ff0000">action</font><font color="#0000ff">="D"</font> <font color="#ff0000">comment</font><font color="#0000ff">=""</font><font color="#0000ff">/></font><br/> <font color="#0000ff"><</font><font color="#800000">event</font> <font color="#ff0000">date</font><font color="#0000ff">="1254728164000"</font> <font color="#ff0000">author</font><font color="#0000ff">="Bin/.svn/entries"</font> <font color="#ff0000">filename</font><font color="#0000ff">="384bccff/ba9fc3f089695f6d/89695f6dea4210c1.c1"</font> <font color="#ff0000">action</font><font color="#0000ff">="D"</font> <font color="#ff0000">comment</font><font color="#0000ff">=""</font><font color="#0000ff">/></font><br/> <font color="#0000ff"><</font><font color="#800000">event</font> <font color="#ff0000">date</font><font color="#0000ff">="1254728164000"</font> <font color="#ff0000">author</font><font color="#0000ff">="Bin/.svn/entries"</font> <font color="#ff0000">filename</font><font color="#0000ff">="486c2459/24e0b8e2d1c311d8/d1c311d80290ed01.01"</font> <font color="#ff0000">action</font><font color="#0000ff">="D"</font> <font color="#ff0000">comment</font><font color="#0000ff">=""</font><font color="#0000ff">/></font><br/> <font color="#0000ff"><</font><font color="#800000">event</font> <font color="#ff0000">date</font><font color="#0000ff">="1254728164000"</font> <font color="#ff0000">author</font><font color="#0000ff">="Bin/.svn/entries"</font> <font color="#ff0000">filename</font><font color="#0000ff">="415eef3b/1c681c2b8a542c77/8a542c77cb1839ce.ce"</font> <font color="#ff0000">action</font><font color="#0000ff">="D"</font> <font color="#ff0000">comment</font><font color="#0000ff">=""</font><font color="#0000ff">/></font><br/> <font color="#0000ff"><</font><font color="#800000">event</font> <font color="#ff0000">date</font><font color="#0000ff">="1254728164000"</font> <font color="#ff0000">author</font><font color="#0000ff">="Bin/.svn/entries"</font> <font color="#ff0000">filename</font><font color="#0000ff">="b3008424/6da995605f28165c/5f28165c84475335.35"</font> <font color="#ff0000">action</font><font color="#0000ff">="D"</font> <font color="#ff0000">comment</font><font color="#0000ff">=""</font><font color="#0000ff">/></font><br/> <font color="#0000ff"><</font><font color="#800000">event</font> <font color="#ff0000">date</font><font color="#0000ff">="1254728164000"</font> <font color="#ff0000">author</font><font color="#0000ff">="Bin/.svn/entries"</font> <font color="#ff0000">filename</font><font color="#0000ff">="ff4d0e6d/ea7152595adb7c97/5adb7c97bf59427e.7e"</font> <font color="#ff0000">action</font><font color="#0000ff">="D"</font> <font color="#ff0000">comment</font><font color="#0000ff">=""</font><font color="#0000ff">/></font><br/> <font color="#0000ff"></</font><font color="#800000">file_events</font><font color="#0000ff">></font></font><br/> <br/> <font color="gray">* This source code was highlighted with <a href="http://virtser.net/blog/post/source-code-highlighter.aspx"><font color="gray">Source Code Highlighter</font></a>.</font>
<font color="black">cat $1 | \<br/> grep -e <font color="#A31515">"event "</font> | \<br/> sed -e <font color="#A31515">"s/^[ ]*//"</font> | \<br/> awk <font color="#A31515">'<br/> $2 ~ /data/ { p1=$2; } <br/> $2 ~ /author/ { p2=$2; } <br/> $2 ~ /action/ { p3=$2;} <br/> $2 ~ /filename/ { p4=$2; } <br/> $2 ~ /comment/ { p5=$2; } <br/> <br/> $3 ~ /data/ { p1=$3; } <br/> $3 ~ /author/ { p2=$3; } <br/> $3 ~ /action/ { p3=$3; } <br/> $3 ~ /filename/ { p4=$3; } <br/> $3 ~ /comment/ { p5=$3; } <br/> <br/> $4 ~ /data/ { p1=$4; } <br/> $4 ~ /author/ { p2=$4; } <br/> $4 ~ /action/ { p3=$4; } <br/> $4 ~ /filename/ { p4=$4; } <br/> $4 ~ /comment/ { p5=$4; } <br/> <br/> $5 ~ /data/ { p1=$5; } <br/> $5 ~ /author/ { p2=$5; } <br/> $5 ~ /action/ { p3=$5; } <br/> $5 ~ /filename/ { p4=$5; } <br/> $5 ~ /comment/ { p5=$5; } <br/> <br/> $6 ~ /data/ { p1=$6; } <br/> $6 ~ /author/ { p2=$6; } <br/> $6 ~ /action/ { p3=$6; } <br/> $6 ~ /filename/ { p4=$6; } <br/> $6 ~ /comment/ { p5=$6; }<br/> <br/> { print p1"|"p2"|"p3"|"p4"|"p5"\n"; } '</font> | \<br/> sort -t <font color="#A31515">"|"</font> -k1 > $result <br/> </font><br/> <font color="gray">* This source code was highlighted with <a href="http://virtser.net/blog/post/source-code-highlighter.aspx"><font color="gray">Source Code Highlighter</font></a>.</font>
<font color="black"><font color="#0000ff">date</font>="1254728164000"|author="Bin/.svn/entries"|<font color="#0000ff">action</font>="D"|filename="f4d64c1a/497b733f81c2866d/81c2866da7e4d268.68"|comment=""/><br/> <font color="#0000ff">date</font>="1254728164000"|author="Bin/.svn/entries"|<font color="#0000ff">action</font>="D"|filename="51d46ff1/fdb0cf112ec24d1e/2ec24d1e87c7a87a.7a"|comment=""/><br/> <font color="#0000ff">date</font>="1254728164000"|author="Bin/.svn/entries"|<font color="#0000ff">action</font>="D"|filename="384bccff/ba9fc3f089695f6d/89695f6dea4210c1.c1"|comment=""/><br/> <font color="#0000ff">date</font>="1254728164000"|author="Bin/.svn/entries"|<font color="#0000ff">action</font>="D"|filename="486c2459/24e0b8e2d1c311d8/d1c311d80290ed01.01"|comment=""/><br/> <font color="#0000ff">date</font>="1254728164000"|author="Bin/.svn/entries"|<font color="#0000ff">action</font>="D"|filename="415eef3b/1c681c2b8a542c77/8a542c77cb1839ce.ce"|comment=""/><br/> <font color="#0000ff">date</font>="1254728164000"|author="Bin/.svn/entries"|<font color="#0000ff">action</font>="D"|filename="b3008424/6da995605f28165c/5f28165c84475335.35"|comment=""/><br/> <font color="#0000ff">date</font>="1254728164000"|author="Bin/.svn/entries"|<font color="#0000ff">action</font>="D"|filename="ff4d0e6d/ea7152595adb7c97/5adb7c97bf59427e.7e"|comment=""/><br/> <font color="#0000ff">date</font>="1254728164000"|author="Bin/.svn/entries"|<font color="#0000ff">action</font>="D"|filename="a0c052d4/b0a0b0c0f70a7d29/f70a7d29231dacbd.bd"|comment=""/><br/> <font color="#0000ff">date</font>="1254728164000"|author="Bin/.svn/entries"|<font color="#0000ff">action</font>="D"|filename="eabd8551/ccb2616f5be66fdb/5be66fdb0d4c9a77.77"|comment=""/><br/> <font color="#0000ff">date</font>="1254728164000"|author="Bin/.svn/entries"|<font color="#0000ff">action</font>="D"|filename="25046ffa/0dfcd577c31d07d8/c31d07d855ade3e5.e5"|comment=""/><br/> <font color="#0000ff">date</font>="1254728164000"|author="Bin/.svn/entries"|<font color="#0000ff">action</font>="D"|filename="cb86925a/bf4f23acb14c6c47/b14c6c474628ff82.82"|comment=""/><br/> <font color="#0000ff">date</font>="1254728164000"|author="Bin/.svn/entries"|<font color="#0000ff">action</font>="D"|filename="51d46ff1/fdb0cf112ec24d1e/2ec24d1e87c7a87a.7a"|comment=""/><br/> <font color="gray">Source Code Highlighter</font></a>.</font>
Answer the question
In order to leave comments, you need to log in
BEGIN {
RS="/>"
}
{
fields = 0;
for(i = 1; i <= NF; i++) {
if($i=="<event") {
fields = 1;
continue;
}
if(!fields) continue;
split($i, a, "[=\"]");
res[a[1]] = a[3];
}
print res["date"]"|"res["author"]"|"res["action"]"|"res["filename"]"|"res["comment"]
}
It's a shame to parse XML with regular expressions.
Try xml-coreutils or XMLStarlet
Maybe use xmllint
from libxml2
(I don’t know if msysgit is included, but, in theory, it should) or windows msxsl
and write a transformation for this task xslt
?
sketched in haste:
xml2 < 1 | sed 's=/file_events/event[/]*[@]*==;' | awk '/^$/{s++}{printf "%05d %s\n",s,$0}' | sort -k1 -k2rn | sed 's/^[^ ]* //;s/[^=]*=//;s/^$/\&\&\&/' | tr '\n' '|' | sed 's/|&&&|/\n/g'
xml2 from the xml2 package. turns xml d like this:
/file_events/event
/file_events/event/@date=1254728164000
/file_events/event/@author=Bin/.svn/entries
/file_events/event/@filename=ff4d0e6d/ea7152595adb7c97/5adb7c97bf59427e.7e
/file_events/event/@action=D
/file_events/event/@comment
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question