Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
最好使用perl或unix命令来解析此字符串_Perl_Unix - Fatal编程技术网

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

最好使用perl或unix命令来解析此字符串,perl,unix,Perl,Unix,是否有一个好的unix one liner或perl liner可以从以下内容格式化此字符串: <?xml version="1.0" encoding="UTF-8"?><org.apache.Summary length="200429142" fileCount="197184" dirCount="50" quota="-1" spaceUsed="601287428" spaceQuota="-1"/> 这是一条单行线,为了清晰起见分为几行: perl -MX

是否有一个好的unix one liner或perl liner可以从以下内容格式化此字符串:

<?xml version="1.0" encoding="UTF-8"?><org.apache.Summary 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-r's/*]+)>/\1/'| tr”“\n

基于@bmk的改进版

sed -r 's/<\?.?*\?>//' | sed -r 's/<[a-z\.]+//I' | \
sed -r 's/\/>//' | sed -r 's/ ([a-z]+)="(-?[0-9]+)"/\1=\2\n/Ig'
sed-r's///'| sed-r's///'| sed-r's/([a-z]+)=“([0-9]+)”/\1=\2\n/Ig”
总共使用了4个
sed

  • 删除
  • 删除
  • 将XML属性提取成对 如果您想就地执行以下操作:

    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
    

    这应该可以满足你的需要

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

    可能吧。到目前为止你有什么,你试过什么?(为什么只有一行?)我会冒险尝试写一个perl脚本。作为一个新手,我会打开一个fh,阅读它,做一个拆分..或者类似的事情,但我确信这太过分了。是的!这在大多数情况下都有效。只需要去掉周围的双引号。伙计,我必须学习“sed”,XML::Parser呢?这个模块也可以使用吗?
     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
    
    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