2011-04-19 20 views
0

是否有一個良好的UNIX一個襯墊或Perl襯墊,可以格式化這個字符串來自:更好地使用Perl或UNIX命令來解析這個字符串

<?xml version="1.0" encoding="UTF-8"?><org.apache.Summary length="200429142" fileCount="197184" dirCount="50" quota="-1" spaceUsed="601287428" spaceQuota="-1"/> 

要:

length=200429142 
filecount=197184 
dirCount=50 
quota=-1 
spaceUsed=601287428 
spaceQuota=-1 
+0

可能。到目前爲止,你有什麼嘗試? (爲什麼一行?) – Mat 2011-04-19 20:50:09

+0

我會冒險,並嘗試寫一個perl腳本。作爲一個新手會打開一個FH,閱讀它,做一個分裂..或類似的東西,但我敢肯定,這是過度殺傷。 – jdamae 2011-04-19 20:55:01

回答

6

這裏有一個-liner,分解成獨立的線清晰:

perl -MXML::Simple -l \ 
    -e '$a = XMLin shift; print "$_=$a->{$_}" for ' \ 
    -e 'qw(length fileCount dirCount quota spaceUsed spaceQuota)' \ 
    (your XML string here) 

這就要求你安裝了XML::Simple模塊。

+0

XML :: Parser怎麼樣?該模塊是否也可以使用? – jdamae 2011-04-20 02:36:40

3

只是一個快速拍攝:這是怎麼回事?

sed -r 's/.*<org.apache.Summary\s+([^>]+)>/\1/' | tr " " "\n" 
+0

yup!大部分工作。只需要去掉周圍的雙引號。男人,我必須學習'sed' – jdamae 2011-04-19 21:16:06

1

基於@bmk

sed -r 's/<\?.?*\?>//' | sed -r 's/<[a-z\.]+//I' | \ 
sed -r 's/\/>//' | sed -r 's/ ([a-z]+)="(-?[0-9]+)"/\1=\2\n/Ig' 

共有4 sed改良版本中使用。

  1. 除去<?xml?>
  2. 除去<org.apache.Summary
  3. 除去/>
  4. 提取XML屬性成對。
2
sed -e 's/.*Summary //;s/\/.*$//' temp|perl -p -e 's/ /\n/g' 

length="200429142" 
fileCount="197184" 
dirCount="50" 
quota="-1" 
spaceUsed="601287428" 
spaceQuota="-1" 
如果你想在地方做

sed -e 's/.*Summary //;s/\/.*$//' temp|perl -pi -e 's/ /\n/g' 

如果DONOT需要"則:

sed -e 's/.*Summary //;s/\/.*$//' temp|perl -p -e 's/ /\n/g;s/\"//g' 
length=200429142 
fileCount=197184 
dirCount=50 
quota=-1 
spaceUsed=601287428 
spaceQuota=-1 
0

這應該做你所需要的。

perl -0777 -E'given(<>){/\?>/g; say "$1$2" while /(\w+=)"(.*?)"/g}' myfile.xml 

輸出

length=200429142 
fileCount=197184 
dirCount=50 
quota=-1 
spaceUsed=601287428 
spaceQuota=-1