如何从一个文件中提取字符串以插入(修改)到另一个文件中?

这些

问题如下:

我有一个包含数据的xml文件,我正在寻找一小部分数据以将其写入新文件:内容已根据请求缩短:

如果type = dhcp-client,则为摘录:

    <deviceconfig>
      <system>
        <type>
          <dhcp-client>
            <send-hostname>yes</send-hostname>
          </dhcp-client>
        </type>
        <hostname>Firewall</hostname>
      </system>
    </deviceconfig>

如果type = static,则为摘录

    <deviceconfig>
      <system>
        <type>
          <static/>
        </type>
        <hostname>Firewall</hostname>
        <permitted-ip>
          <entry name="192.168.0.0/24"/>
        </permitted-ip>
        <ip-address>192.168.0.2</ip-address>
        <netmask>255.255.255.0</netmask>
        <default-gateway>192.168.0.1</default-gateway>
      </system>
    <network>
      <interface>
        <ethernet>
          <entry name="ethernet1/1">
            <layer3>
              <ip>
                <entry name="192.168.0.5/24"/>
              </ip>
            </layer3>
          </entry>
        </ethernet>
      </interface>
      <virtual-router>
        <entry name="default">
          <routing-table>
            <ip>
              <static-route>
                <entry name="default-route">
                  <nexthop>
                    <ip-address>192.168.0.1</ip-address>
                  </nexthop>
                  <interface>ethernet1/4</interface>
                  <destination>0.0.0.0/0</destination>
                </entry>
              </static-route>
            </ip>
          </routing-table>
        </entry>
      </virtual-router>
    </network>

四个相关值在“系统”标签中是唯一的(或不存在),<system></system>例如ip-address之类的东西可能会在其他地方再次出现,<system></system>但我只是检查系统内部的那些,如果类型不是静态的,则不要显示,我对其进行设置到dhcp-client

这是我需要的结果,如果类型为dhcp,则在文件中:

type=dhcp-client

如果类型是静态的,这就是我需要的结果:

type=static
ip-address=192.168.0.2
default-gateway=192.168.0.1
netmask=255.255.255.0

我不确定如何有效地完成此工作并将其集成到现有的PHP文件中(因此可以使用exec或更好地使用php)。

我还限于默认情况下在ubuntu服务器系统上安装的工具,并且将无法使用其他软件包。

PS:这实际上是整个/完整的用例,除了这两个示例外,我将不需要产生其他输出。感谢您的帮助或指点:)

这些

好吧,我自己找到了答案...

实际上,在PHP中要比没有它简单得多...

确实花了我几个小时才能完成^^

#load the file as simplexml object and then switch into system
#https://www.w3schools.com/php/func_simplexml_load_file.asp
$xml=simplexml_load_file('./myfile') or die("Error: Cannot create object");
$xml=$xml->system

#put the whole string(s) into a variable, getname gets the name of the object itself if it exists
#https://www.w3schools.com/php/func_simplexml_getname.asp
$output='type=' . $xml -> type -> static -> getName() . $xml -> type -> {'dhcp-client'} -> getName() . "\nip-address=" . $xml -> {'ip-address'} . "\ndefault-gateway=" . $xml -> {'default-gateway'} . "\nnetmask=" . $xml -> netmask;

#write the output into a file
#https://www.w3schools.com/php/func_filesystem_file_put_contents.asp
file_put_contents('./myoutputfile', $output );

这为我的第一个代码段提供了以下输出(如果没有给出值,则后三行就可以了,否则我可以检查它们是否首先存在):

type=dhcp-client
ip-address=
default-gateway=
netmask=

第二个代码段的输出如下:

type=static
ip-address=192.168.0.2
default-gateway=192.168.0.1
netmask=255.255.255.0

谢谢大家的帮助:)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何从一个文本文件到另一个文本文件逐行提取字符串

如何使用 Bash 从一个文件中提取一系列字符串并替换另一个文件中的一系列单独的字符串?

从一个文件中读取字符串,grep在另一个文件中的第一个匹配项

如何从一个单词到另一个单词提取字符串的一部分?

如何从一个文本文件中提取行,该文本文件包含另一个文件中的列表中的字符串?

从Javascript中的另一个字符串中提取字符串

根据R中另一个向量中的字符从字符串中提取字符

从一个文本文件中提取两个不同的 JSON 字符串到一个 JSON 数组中

powershell 从变量中提取字符串以分配另一个变量

从一个字符读取字符串到另一个

在 C# 中使用另一个文件从一个文件中删除字符串?

从一个文件中提取数据并保存到另一个文件

检查文件中的字符串,提取变量并写入另一个文件

如何在iOS中将字符串值从一个.m文件传递到另一个.m文件

从另一个以特殊字符开头和结尾的字符串中提取字符串

如何根据另一个文件的ID从一个文件中提取数据

如何从一个文件中提取列并移动到另一个文件而不打印列?

基于另一个文件中的值从一个文件中提取行-LINUX

从一个文件中提取与另一个文件中的行相对应的数据

使用grep从另一个字符串中提取字符串

(从一个文件中)搜索另一个文件中的多个字符串并列出缺少的字符串

如何从一个字符串的第一次出现开始从一个大文件中提取文本?

如果匹配另一个列表中的值,则提取字符串

如何从一个.txt文件中提取一堆行并将其添加到另一个文件中?

从文件中提取键/值到UNIX中的另一个文件

从一个文件中查找另一个文件中的字符串(如果不存在),然后从原始文件中删除

从一个jsp返回字符串到另一个

如何从字符串中提取一个或多个单词并在两个不同的列中搜索它们以形成 R 中的另一个文件

基于python中的另一个字符串从列表中提取字符串