Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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脚本拆分xml文件_Xml_Regex_Perl - Fatal编程技术网

使用perl脚本拆分xml文件

使用perl脚本拆分xml文件,xml,regex,perl,Xml,Regex,Perl,嗨,我正在使用perl脚本将大xml分解成小块。我已经推荐了这个链接 我的代码是这样的 if($line =~ /^</row>/) { $count++; } if($line=~/^/) { $count++; } 但是我得到了这个错误 works\filesplit.pl line 20. Bareword found where operator expected at E:\Work\perl works\filesplit.pl line 2 0, near "/

嗨,我正在使用perl脚本将大xml分解成小块。我已经推荐了这个链接

我的代码是这样的

if($line =~ /^</row>/)
{
$count++;
}
if($line=~/^/)
{
$count++;
}
但是我得到了这个错误

 works\filesplit.pl line 20.
Bareword found where operator expected at E:\Work\perl works\filesplit.pl line 2
0, near "/^</row"
        (Missing operator before row?)
syntax error at E:\Work\perl works\filesplit.pl line 20, near "/^</row"
Search pattern not terminated at E:\Work\perl works\filesplit.pl line 20.
works\filesplit.pl第20行。
Bareword在E:\Work\perl works\filesplit.pl第2行找到了运算符所需的位置
0,在“/^附近,如果您试图在行首匹配
,则需要
^
。这是我的测试代码

#!/usr/bin/perl
use strict;
use warnings;

my $line = "</row> something";
if ($line =~ /^<\/row>/)
{
    print "found a match \n";
}
更新

在OP提供样本数据后发布此更新

您的正则表达式中需要
^\s+
,因为它们并非都从行的开头开始。其中一些在它们前面有
一个空格
。因此,在进行实际匹配之前,我们需要在行的开头匹配零个或多个空格

代码:


您试过了吗?这是一个专门设计用于根据各种标准(标记名、级别、大小)拆分大型XML文件的工具。

以下内容可能会有所帮助:

use strict;
use warnings;

my $i = 1;
local $/ = '<row>';

while (<>) {
    chomp;
    s!</row>!! or next;

    open my $fh, '>', 'File_' . ( sprintf '%05d', $i++ ) . '.xml' or die $!;
    print $fh $_;
}
使用严格;
使用警告;
我的$i=1;
本地$/='';
而(){
咀嚼;
s!!!或者下一个;
打开我的$fh、'>'、'File.'(sprintf'%05d'、$i++..xml'或die$!;
打印$fh$;
}
用法:
perl script.pl infle.xml

这将Perl的记录分隔符
$/
设置为
,以读取由
分隔的那些“块”中的xml文件。它从块中删除
,然后将该块写入一个命名方案为“file.\nnnnn.xml”的文件。

!/bin/Perl-w
##使用perl脚本拆分xml文件
“打印”输入文件?";
chomp($XmlFile=);
打开$XmlFileHandle','OutputFile.'.$splitby;
##被…分开。。。
while(){
如果(//){
打印$outputhHandle“\n”;
最后;
}
}
while(){
$line=$\;
如果($line=~m//){
打印$outputhHandle“”;
最后;
}
打印$OutputHandle$行;
}
打印“\n输出文件为:输出文件\u$splitby\n”;

您希望如何将文件“分块”以及如何处理这些分块?@Kenosis…”5" ........ 分块file@Kenosis .. 实际上我的文件太大了,所以我想把它分块。。在单个文件中。。。。就像这样,我的屏幕变得空白。没有发生任何情况检查目录中生成的文件。
# perl test.pl 
found a match 
#!/usr/bin/perl -w
use strict;
use warnings;

while (my $line = <DATA>)
{
    if ($line =~ /^\s+<\/row>/)
    {
        print "found a match \n";
    }
}

__DATA__
<row>
  <date></date>
  <ForeignpostingId />
  <country>11</country>
  <domain>http://www.xxxx.com</domain>
  <domainid>20813</domainid>
 </row>
 <row>
  <date></date>
  <ForeignpostingId />
  <country>11</country>
  <domain>http://www.xxxx.com</domain>
  <domainid>20813</domainid>
 </row>
 <row>
  <date></date>
  <ForeignpostingId />
  <country>11</country>
  <domain>http://www.xxxx.com</domain>
  <domainid>20813</domainid>
 </row>
# perl test.pl 
found a match 
found a match 
found a match 
use strict;
use warnings;

my $i = 1;
local $/ = '<row>';

while (<>) {
    chomp;
    s!</row>!! or next;

    open my $fh, '>', 'File_' . ( sprintf '%05d', $i++ ) . '.xml' or die $!;
    print $fh $_;
}
#!/bin/perl -w

## splitting xml files using perl script

print "Input File ? ";
chomp($XmlFile = <STDIN>);

open $XmlFileHandle,'<',$XmlFile;

print "\nSplit By which Tag ? ";
chomp($splitby = <STDIN>);

open $OutputHandle, '>','OutputFile_'.$splitby;

## to split by <user>...</user>
while(<$XmlFileHandle>){
    if(/<$splitby>/){
        print $OutputHandle "<$splitby>\n";
        last;
    }
}

while(<$XmlFileHandle>){
    $line = $_;
    if($line =~ m/<\/$splitby>/){
        print $OutputHandle "</$splitby>";
        last;
    }
    print $OutputHandle $line;
}

print "\nOutput File is : OutputFile_$splitby\n";