Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.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
Bash Unix脚本,用于将一个大文件拆分为多个文件,每个文件中有两对标记,且文件名上有命名约定_Bash_Shell_Unix - Fatal编程技术网

Bash Unix脚本,用于将一个大文件拆分为多个文件,每个文件中有两对标记,且文件名上有命名约定

Bash Unix脚本,用于将一个大文件拆分为多个文件,每个文件中有两对标记,且文件名上有命名约定,bash,shell,unix,Bash,Shell,Unix,我正在编写一个Shell脚本,将一个大文件拆分为多个文件,每个文件中有两对标记,这些小文件名必须遵循命名约定。 例如:- 大文件名:abcdef123.xml 内容: <parent> <child> <code1><code1> <text1><text1> </child> <child1> <code2><

我正在编写一个Shell脚本,将一个大文件拆分为多个文件,每个文件中有两对标记,这些小文件名必须遵循命名约定。

例如:-

大文件名:abcdef123.xml 内容:

<parent>
    <child>
        <code1><code1>
        <text1><text1>
    </child>
    <child1>
        <code2><code2>
        <text2><text2>
    </child1>
    <child>
        <code3><code3>
        <text3><text3>
    </child>
    <child1>
        <code4><code4>
        <text4><text4>
    </child1>
    <child>
        <code5><code5>
        <text5><text5>
    </child>
    <child1>
        <code6><code6>
        <text6><text6>
    </child1>
    <child>
        <code7><code7>
        <text7><text7>
    </child>
    <child1>
        <code8><code8>
        <text8><text8>
    </child1>
</parent>
文件2=stack_10132020134434791_002.xml

内容:-

<parent>
    <child>
        <code1><code1>
        <text1><text1>
    </child>
    <child1>
        <code2><code2>
        <text2><text2>
    </child1>
    <child>
        <code3><code3>
        <text3><text3>
    </child>
    <child1>
        <code4><code4>
        <text4><text4>
    </child1>
</parent>
<parent>
    <child>
        <code5><code5>
        <text5><text5>
    </child>
    <child1>
        <code6><code6>
        <text6><text6>
    </child1>
    <child>
        <code7><code7>
        <text7><text7>
    </child>
    <child1>
        <code8><code8>
        <text8><text8>
    </child1>
</parent>
sample.xml:


aa
aat
aa2
aat2
bb
bbt
bb2
bbt2
复写的副本
cct
cc2
cct2
dd
滴滴涕
二阶插值滤波
ddt2
parser.sh

#/bin/bash
PARENT='PARENT'
CHILD1='child'
CHILD2='child1'
输入文件=“sample.xml”
NUM_OF_CHILDS=$(cat$INPUT_文件| grep”“| wc-l)
文件数量=1
以美元为单位的i(儿童数量为12美元);做
回声“--------------------------------------------------------------”
回显“文件名”$(日期+%s%N)“\u$文件\u NUM.xml”
回声“--------------------------------------------------------------”
回声“
回声“
xmllint--xpath“(//parent/$CHILD1[$i])”$INPUT\u文件
xmllint--xpath“(//parent/$CHILD2[$i])”$INPUT\u文件
xmllint--xpath“(//parent/$CHILD1[$((i+1))])”$INPUT\u文件
xmllint--xpath“(//parent/$CHILD2[$((i+1))])”$INPUT\u文件
回声“
文件数量=$((文件数量+1))
完成
输出:

-----------------------------------------------------
FILENAME_1603633647540475038_1.xml
-----------------------------------------------------

aa
aat
aa2
aat2
bb
bbt
bb2
bbt2

复写的副本
cct
cc2
cct2
dd
滴滴涕
二阶插值滤波
ddt2
看起来您计划将输出文件作为xml使用,所以缩进和换行并不重要。在其他情况下,尝试使用
xmllint
参数。

其他细节,如文件命名约定,很容易更改,所以这取决于您。

您需要使用专用的XML工具,如
xmlstarlet
,或者使用带有适当XML解析器的语言编写脚本,如Python。用普通正则表达式是无法实现的。谢谢。它对我有用。但是,我想知道如何从中创建文件。我的意思是“FILENAME_1603633647540475038_1.xml”和“FILENAME_1603633647547254647_2.xml”需要在目录中作为两个单独的文件创建,但我只能在Linux的Linux工作空间中作为输出使用
echo“some output”>/your_directory/FILENAME_$(日期+%s%N)_$FILE_NUM.xml
以重定向文件中的输出,而不是标准输出
-----------------------------------------------------
FILENAME_1603633647540475038_1.xml
-----------------------------------------------------
-----------------------------------------------------
FILENAME_1603633647547254647_2.xml
-----------------------------------------------------