繁体   English   中英

最好使用perl或unix命令来解析此字符串

[英]better to use perl or unix commands to parse this string

是否有一个好的UNIX内衬或perl内衬可以格式化以下字符串:

<?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

这是一个单线,为了清晰起见,分为几行:

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模块。

只是一个快速镜头:那呢?

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

如果您不需要"则:

 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

基于@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属性成对提取。

这应该做您需要的。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM