使用 bash 脚本将 XML 文件的内容添加到另一个文件

使用 bash 脚本将 XML 文件的内容添加到另一个文件

我有两个 XML 文件,第一个是~/tmp/test.xml第二个,/data/myuser/.mycontent/mytest.xml我想将第一个 XML 文件的所有内容添加到第二个 XML 文件的第 35 行。我尝试了以下但没有运气

sed -n '35,~/tmp/test.xml`' /data/myuser/.mycontent/mytest.xml

(cat /data/myuser/.mycontent/mytest.xml; echo) | sed '35r ~/tmp/test.xml'

ed -s ~/tmp/test.xml <<< $'35r /data/myuser/.mycontent/mytest.xml\nw'

第二个 XML 文件第 34 行的第 33 行为空

#the following tags contain employee location

第一个 XML 文件中的 XML 标记

<Location "/mylocation">
    first Address
    second Address

    Mylocation "XX/XX/XX/XX"
    Myphone "XXXXXXX"
</Location>

我做错了什么,请指教。

编辑1

第一个 XML~/tmp/test.xml文件仅包含

<Location "/mylocation">
    first Address
    second Address

    Mylocation "XX/XX/XX/XX"
    Myphone "XXXXXXX"
</Location>

第二个 XML/data/myuser/.mycontent/mytest.xml包含:

NameVirtualHost *:XXXX
<VirtualHost  *:XXXX>

    ServerName AAAAAAAA

# Manager comment 1
# Manager comment 2
# Manager comment 3
#
DocumentRoot "/data/myuser/.mycontent/"

# support email [email protected]
# started at 2010
<employee /*>
        AllowOverride None
</employee>

<Location "/">
        mylocation
        Deny from all
</Location>

<Location "/icons/">
#        employee info
        my employee info
        Allow from all
</Location>

DavLockDB /tmp/${APACHE_HOSTNAME}.DavLock
DAVMinTimeout 5000
LimitXMLRequestBody 0

# This should be changed to whatever you set DocumentRoot to.

## I need to add new tags here ##
<Location "/employee1">
    first Address
    second Address

    Mylocation "XX/XX/XX/XX"
    Myphone "XXXXXXX"
</Location>

<Location "/employee2">
    first Address
    second Address

    Mylocation "XX/XX/XX/XX"
    Myphone "XXXXXXX"
</Location>
## more tags same as above
## then manager comment

编辑2 第二个文件/data/myuser/.mycontent/mytest.xml应该是这样的:

 NameVirtualHost *:XXXX
    <VirtualHost  *:XXXX>

        ServerName AAAAAAAA

    # Manager comment 1
    # Manager comment 2
    # Manager comment 3
    #
    DocumentRoot "/data/myuser/.mycontent/"

    # support email [email protected]
    # started at 2010
    <employee /*>
            AllowOverride None
    </employee>

    <Location "/">
            mylocation
            Deny from all
    </Location>

    <Location "/icons/">
    #        employee info
            my employee info
            Allow from all
    </Location>

    DavLockDB /tmp/${APACHE_HOSTNAME}.DavLock
    DAVMinTimeout 5000
    LimitXMLRequestBody 0

    # This should be changed to whatever you set DocumentRoot to.

    ## I need to add new tags here ##
  ## this tag from first file   
  <Location "/mylocation">
        first Address
        second Address

        Mylocation "XX/XX/XX/XX"
        Myphone "XXXXXXX"
    </Location>

  ## edit end

    <Location "/employee1">
        first Address
        second Address

        Mylocation "XX/XX/XX/XX"
        Myphone "XXXXXXX"
    </Location>

    <Location "/employee2">
        first Address
        second Address

        Mylocation "XX/XX/XX/XX"
        Myphone "XXXXXXX"
    </Location>
    ## more tags same as above
    ## then manager comment

注:## this tag from first file## edit end指定合并位置位置

答案1

使用 GNU sed:

如果您想在 file2.xml 中的第 35 行插入 file1.xml 并带有前导换行符:

sed -e '34{p;g;r file1.xml' -e '}' file2.xml

如果您想“就地”编辑 file2.xml,请添加 sed 的选项-i

答案2

好的,这不是像我想象的那样将 XML 插入到 XML 中 - 如果是的话,答案将是“使用解析器”。然而事实并非如此,您只是将一个文本文件合并到另一个文本文件中。

因此,我会像perl往常一样:

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

open ( my $insert, '<', '~/tmp/test.xml' ) or die $!;
open ( my $modify, '<', '/data/myuser/.mycontent/mytest.xml' ) or die $!; 
open ( my $output, '>', '/data/myuser/.mycontent/mytest.xml.new' ) or die $!; 

select $output; 
while ( <$modify> ) { 
   if ( $. == 32 ) { print <$insert>; }; 
   print; 
}

这应该可以解决问题 - 如果你想要一个衬垫,那么它可以压缩为:

perl -p -i.bak -e 'BEGIN { open ( $insert, "<", shift ) } if ( $. == 32 ) { print <$insert> }' ~/tmp/test.xml /data/myuser/.mycontent/mytest.xml

注意$.perl“当前行号”。如果您愿意,可以应用不同类型的条件。就像正则表达式是否匹配(这可能更合适,给定的配置文件往往会插入行)。

答案3

我想我理解你正在尝试做的问题。看起来您可能想r在输入提示上插入您的 ead 文件。你说第 35 行,但如果你sed 35r\ file这样做,它将被附加到35.很难观察提示的输入如果您不确保前瞻,则需要写入输出。例如:

sed -e'$!N;/\n<Location "\/employee1">/r /tmp/file1' \
    -eP\;D </tmp/file2 | tail -n30

现在扫描输入<Location /employee1>file1并在找到之前立即插入内容。


DAVMinTimeout 5000
LimitXMLRequestBody 0

# This should be changed to whatever you set DocumentRoot to.

## I need to add new tags here ##
<Location "/mylocation">
    first Address
    second Address

    Mylocation "XX/XX/XX/XX"
    Myphone "XXXXXXX"
</Location>
<Location "/employee1">
    first Address
    second Address

    Mylocation "XX/XX/XX/XX"
    Myphone "XXXXXXX"
</Location>

<Location "/employee2">
    first Address
    second Address

    Mylocation "XX/XX/XX/XX"
    Myphone "XXXXXXX"
</Location>
## more tags same as above
## then manager comment

相关内容