如何在带有变量的文件中的某个字符串后插入文本

发布于 2025-01-26 19:19:12 字数 1498 浏览 2 评论 0原文

我有这些变量:

file_path="/home/dir/file.xml"
string="<host mac='0A:00:DD:D2:00:02' name='vfp-20vr' ip='10.10.1.122'/>"

这是file.xml的内容,

<network>
  <name>br-ext</name>
  <forward mode='route'/>
  <bridge name='br-ext' stp='on' delay='0'/>
  <mac address='52:54:00:9f:a0:00'/>
  <ip address='10.10.1.11' netmask='255.255.255.0'>
    <dhcp>
      <host mac='0A:AA:FF:C1:A1:EE' name='vsrxa-1' ip='10.10.1.21'/>
      <host mac='0A:AA:FF:C1:A2:EE' name='vsrxa-2' ip='10.10.1.22'/>
      <host mac='0A:AA:FF:C1:B1:EE' name='vsrxb-1' ip='10.10.1.23'/>
      <host mac='0A:AA:FF:C1:B2:EE' name='vsrxb-2' ip='10.10.1.24'/>
      <host mac='0A:AA:FF:C1:C1:EE' name='vsrxc-1' ip='10.10.1.25'/>
      <host mac='0A:AA:FF:C1:C2:EE' name='vsrxc-2' ip='10.10.1.26'/>
      <host mac='0A:AA:FF:C1:D1:EE' name='vsrxd-1' ip='10.10.1.27'/>
      <host mac='0A:AA:FF:C1:D2:EE' name='vsrxd-2' ip='10.10.1.28'/>
    </dhcp>
  </ip>
</network>

我想在$ lt; dhcp&gt; flag之后立即添加$ string。 ,类似的事情:

...
<dhcp>
      <host mac='0A:00:DD:D2:00:02' name='vfp-20vr' ip='10.10.1.122'/>
      <host mac='0A:AA:FF:C1:A1:EE' name='vsrxa-1' ip='10.10.1.21'/>
...

我尝试使用sed“/\ dhcp \/a $ string $ file_path”在我的bash脚本中没有成功...

有人知道我如何实现这一目标吗?

I have these variables:

file_path="/home/dir/file.xml"
string="<host mac='0A:00:DD:D2:00:02' name='vfp-20vr' ip='10.10.1.122'/>"

This is the content of file.xml

<network>
  <name>br-ext</name>
  <forward mode='route'/>
  <bridge name='br-ext' stp='on' delay='0'/>
  <mac address='52:54:00:9f:a0:00'/>
  <ip address='10.10.1.11' netmask='255.255.255.0'>
    <dhcp>
      <host mac='0A:AA:FF:C1:A1:EE' name='vsrxa-1' ip='10.10.1.21'/>
      <host mac='0A:AA:FF:C1:A2:EE' name='vsrxa-2' ip='10.10.1.22'/>
      <host mac='0A:AA:FF:C1:B1:EE' name='vsrxb-1' ip='10.10.1.23'/>
      <host mac='0A:AA:FF:C1:B2:EE' name='vsrxb-2' ip='10.10.1.24'/>
      <host mac='0A:AA:FF:C1:C1:EE' name='vsrxc-1' ip='10.10.1.25'/>
      <host mac='0A:AA:FF:C1:C2:EE' name='vsrxc-2' ip='10.10.1.26'/>
      <host mac='0A:AA:FF:C1:D1:EE' name='vsrxd-1' ip='10.10.1.27'/>
      <host mac='0A:AA:FF:C1:D2:EE' name='vsrxd-2' ip='10.10.1.28'/>
    </dhcp>
  </ip>
</network>

I would like to add the $string right after the <dhcp> flag, something like this:

...
<dhcp>
      <host mac='0A:00:DD:D2:00:02' name='vfp-20vr' ip='10.10.1.122'/>
      <host mac='0A:AA:FF:C1:A1:EE' name='vsrxa-1' ip='10.10.1.21'/>
...

I tried using sed "/\dhcp\/a $string $file_path" in my bash script with no success...

Does anyone know how I can achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

情愿 2025-02-02 19:19:12

您不太远:

sed -i "/<dhcp>/a \ \ \ \ \ \ ${string}" "${file_path}"
  • -i更改输入文件
  • /&lt; dhcp&gt;/仅选择dhcp open tag
  • \ code> \) * 6到凹痕字符串内容

结果:

<network>
  <name>br-ext</name>
  <forward mode='route'/>
  <bridge name='br-ext' stp='on' delay='0'/>
  <mac address='52:54:00:9f:a0:00'/>
  <ip address='10.10.1.11' netmask='255.255.255.0'>
    <dhcp>
      <host mac='0A:00:DD:D2:00:02' name='vfp-20vr' ip='10.10.1.122'/>
      <host mac='0A:AA:FF:C1:A1:EE' name='vsrxa-1' ip='10.10.1.21'/>
      <host mac='0A:AA:FF:C1:A2:EE' name='vsrxa-2' ip='10.10.1.22'/>
      <host mac='0A:AA:FF:C1:B1:EE' name='vsrxb-1' ip='10.10.1.23'/>
      <host mac='0A:AA:FF:C1:B2:EE' name='vsrxb-2' ip='10.10.1.24'/>
      <host mac='0A:AA:FF:C1:C1:EE' name='vsrxc-1' ip='10.10.1.25'/>
      <host mac='0A:AA:FF:C1:C2:EE' name='vsrxc-2' ip='10.10.1.26'/>
      <host mac='0A:AA:FF:C1:D1:EE' name='vsrxd-1' ip='10.10.1.27'/>
      <host mac='0A:AA:FF:C1:D2:EE' name='vsrxd-2' ip='10.10.1.28'/>
    </dhcp>
  </ip>
</network>

You are not too far:

sed -i "/<dhcp>/a \ \ \ \ \ \ ${string}" "${file_path}"
  • -i to change input file
  • /<dhcp>/ to select only dhcp open tag
  • (\) * 6 to indent string content

Result:

<network>
  <name>br-ext</name>
  <forward mode='route'/>
  <bridge name='br-ext' stp='on' delay='0'/>
  <mac address='52:54:00:9f:a0:00'/>
  <ip address='10.10.1.11' netmask='255.255.255.0'>
    <dhcp>
      <host mac='0A:00:DD:D2:00:02' name='vfp-20vr' ip='10.10.1.122'/>
      <host mac='0A:AA:FF:C1:A1:EE' name='vsrxa-1' ip='10.10.1.21'/>
      <host mac='0A:AA:FF:C1:A2:EE' name='vsrxa-2' ip='10.10.1.22'/>
      <host mac='0A:AA:FF:C1:B1:EE' name='vsrxb-1' ip='10.10.1.23'/>
      <host mac='0A:AA:FF:C1:B2:EE' name='vsrxb-2' ip='10.10.1.24'/>
      <host mac='0A:AA:FF:C1:C1:EE' name='vsrxc-1' ip='10.10.1.25'/>
      <host mac='0A:AA:FF:C1:C2:EE' name='vsrxc-2' ip='10.10.1.26'/>
      <host mac='0A:AA:FF:C1:D1:EE' name='vsrxd-1' ip='10.10.1.27'/>
      <host mac='0A:AA:FF:C1:D2:EE' name='vsrxd-2' ip='10.10.1.28'/>
    </dhcp>
  </ip>
</network>
夏日浅笑〃 2025-02-02 19:19:12

使用sed

$ sed  "/<dhcp>/{p;s|\( \+\).*|\1  $string|;}" file
<network>
  <name>br-ext</name>
  <forward mode='route'/>
  <bridge name='br-ext' stp='on' delay='0'/>
  <mac address='52:54:00:9f:a0:00'/>
  <ip address='10.10.1.11' netmask='255.255.255.0'>
    <dhcp>
      <host mac='0A:00:DD:D2:00:02' name='vfp-20vr' ip='10.10.1.122'/>
      <host mac='0A:AA:FF:C1:A1:EE' name='vsrxa-1' ip='10.10.1.21'/>
      <host mac='0A:AA:FF:C1:A2:EE' name='vsrxa-2' ip='10.10.1.22'/>
      <host mac='0A:AA:FF:C1:B1:EE' name='vsrxb-1' ip='10.10.1.23'/>
      <host mac='0A:AA:FF:C1:B2:EE' name='vsrxb-2' ip='10.10.1.24'/>
      <host mac='0A:AA:FF:C1:C1:EE' name='vsrxc-1' ip='10.10.1.25'/>
      <host mac='0A:AA:FF:C1:C2:EE' name='vsrxc-2' ip='10.10.1.26'/>
      <host mac='0A:AA:FF:C1:D1:EE' name='vsrxd-1' ip='10.10.1.27'/>
      <host mac='0A:AA:FF:C1:D2:EE' name='vsrxd-2' ip='10.10.1.28'/>
    </dhcp>
  </ip>
</network>

Using sed

$ sed  "/<dhcp>/{p;s|\( \+\).*|\1  $string|;}" file
<network>
  <name>br-ext</name>
  <forward mode='route'/>
  <bridge name='br-ext' stp='on' delay='0'/>
  <mac address='52:54:00:9f:a0:00'/>
  <ip address='10.10.1.11' netmask='255.255.255.0'>
    <dhcp>
      <host mac='0A:00:DD:D2:00:02' name='vfp-20vr' ip='10.10.1.122'/>
      <host mac='0A:AA:FF:C1:A1:EE' name='vsrxa-1' ip='10.10.1.21'/>
      <host mac='0A:AA:FF:C1:A2:EE' name='vsrxa-2' ip='10.10.1.22'/>
      <host mac='0A:AA:FF:C1:B1:EE' name='vsrxb-1' ip='10.10.1.23'/>
      <host mac='0A:AA:FF:C1:B2:EE' name='vsrxb-2' ip='10.10.1.24'/>
      <host mac='0A:AA:FF:C1:C1:EE' name='vsrxc-1' ip='10.10.1.25'/>
      <host mac='0A:AA:FF:C1:C2:EE' name='vsrxc-2' ip='10.10.1.26'/>
      <host mac='0A:AA:FF:C1:D1:EE' name='vsrxd-1' ip='10.10.1.27'/>
      <host mac='0A:AA:FF:C1:D2:EE' name='vsrxd-2' ip='10.10.1.28'/>
    </dhcp>
  </ip>
</network>
猫卆 2025-02-02 19:19:12

您可以使用模式匹配&lt; dhcp&gt;零件并捕获领先的空间。

然后附加下一个字符串,用完整的匹配来代替最后一个模式,然后再进行新线和反向注册,以保持相同的缩进。

从那时起,您可以用自己的缩进来扩展它。

sed "/^\([[:space:]]*\)<dhcp>/{N;s##&\n\1  $string#}" "$file_path"

说明

  • /^\([[:SPACE:]]*\) ;
  • n将下一行附加到模式空间
  • s替代
  • ##最后匹配的模式(我更改了定界符替代
  • &amp; \ n \ 1替换为完整匹配,newline和对包含缩进的组1的反向注册并添加您自己的缩进

输出

<network>
  <name>br-ext</name>
  <forward mode='route'/>
  <bridge name='br-ext' stp='on' delay='0'/>
  <mac address='52:54:00:9f:a0:00'/>
  <ip address='10.10.1.11' netmask='255.255.255.0'>
    <dhcp>
      <host mac='0A:00:DD:D2:00:02' name='vfp-20vr' ip='10.10.1.122'/>
      <host mac='0A:AA:FF:C1:A1:EE' name='vsrxa-1' ip='10.10.1.21'/>
      <host mac='0A:AA:FF:C1:A2:EE' name='vsrxa-2' ip='10.10.1.22'/>
      <host mac='0A:AA:FF:C1:B1:EE' name='vsrxb-1' ip='10.10.1.23'/>
      <host mac='0A:AA:FF:C1:B2:EE' name='vsrxb-2' ip='10.10.1.24'/>
      <host mac='0A:AA:FF:C1:C1:EE' name='vsrxc-1' ip='10.10.1.25'/>
      <host mac='0A:AA:FF:C1:C2:EE' name='vsrxc-2' ip='10.10.1.26'/>
      <host mac='0A:AA:FF:C1:D1:EE' name='vsrxd-1' ip='10.10.1.27'/>
      <host mac='0A:AA:FF:C1:D2:EE' name='vsrxd-2' ip='10.10.1.28'/>
    </dhcp>
  </ip>
</network>

You could use a pattern to match the <dhcp> part and capture the leading spaces.

Then append the next string, substitute with the full match for the last pattern followed by a newline and the backreference to keep the same indenting.

From that point on, you can extend it with your own indenting.

sed "/^\([[:space:]]*\)<dhcp>/{N;s##&\n\1  $string#}" "$file_path"

Explanation

  • /^\([[:space:]]*\)<dhcp>/ Natch start of string, capture optional spaces and match <dhcp>
  • N Append the next line to the pattern space
  • s substitute
  • ## The last matched pattern (I have changed the delimiter of the substitute to #)
  • &\n\1 Replace with the full match, newline and a backreference to group 1 containing the indenting and add your own indenting

Output

<network>
  <name>br-ext</name>
  <forward mode='route'/>
  <bridge name='br-ext' stp='on' delay='0'/>
  <mac address='52:54:00:9f:a0:00'/>
  <ip address='10.10.1.11' netmask='255.255.255.0'>
    <dhcp>
      <host mac='0A:00:DD:D2:00:02' name='vfp-20vr' ip='10.10.1.122'/>
      <host mac='0A:AA:FF:C1:A1:EE' name='vsrxa-1' ip='10.10.1.21'/>
      <host mac='0A:AA:FF:C1:A2:EE' name='vsrxa-2' ip='10.10.1.22'/>
      <host mac='0A:AA:FF:C1:B1:EE' name='vsrxb-1' ip='10.10.1.23'/>
      <host mac='0A:AA:FF:C1:B2:EE' name='vsrxb-2' ip='10.10.1.24'/>
      <host mac='0A:AA:FF:C1:C1:EE' name='vsrxc-1' ip='10.10.1.25'/>
      <host mac='0A:AA:FF:C1:C2:EE' name='vsrxc-2' ip='10.10.1.26'/>
      <host mac='0A:AA:FF:C1:D1:EE' name='vsrxd-1' ip='10.10.1.27'/>
      <host mac='0A:AA:FF:C1:D2:EE' name='vsrxd-2' ip='10.10.1.28'/>
    </dhcp>
  </ip>
</network>
紫﹏色ふ单纯 2025-02-02 19:19:12

尝试一下

#! /bin/bash

file_path="$HOME/test.xml"
content="<host>b</host>"

c=$(echo $content | sed 's/\//\\\//g')
sed "/<\/dhcp>/ s/.*/${c}\n&/" $file_path

try this

#! /bin/bash

file_path="$HOME/test.xml"
content="<host>b</host>"

c=$(echo $content | sed 's/\//\\\//g')
sed "/<\/dhcp>/ s/.*/${c}\n&/" $file_path
∞梦里开花 2025-02-02 19:19:12

使用bashsed's a(append)命令:

sed '/^[[:blank:]]*<dhcp>[[:blank:]]*$/a'
\\\n'"$string" "$file_path"

Using bash and sed's a (append) command:

sed '/^[[:blank:]]*<dhcp>[[:blank:]]*$/a'
\\\n'"$string" "$file_path"
陈甜 2025-02-02 19:19:12
strings="      STRING1...\n      STRING2...\n      ...\n" 
match="<dhcp>"  
awk -v m="$match" -v s="$strings" '{ print; if($1 == m) printf s }' input_file

<network>
  <name>br-ext</name>
  <forward mode='route'/>
  <bridge name='br-ext' stp='on' delay='0'/>
  <mac address='52:54:00:9f:a0:00'/>
  <ip address='10.10.1.11' netmask='255.255.255.0'>
    <dhcp>
      STRING1...
      STRING2...
      ...
      <host mac='0A:AA:FF:C1:A1:EE' name='vsrxa-1' ip='10.10.1.21'/>
      <host mac='0A:AA:FF:C1:A2:EE' name='vsrxa-2' ip='10.10.1.22'/>
      <host mac='0A:AA:FF:C1:B1:EE' name='vsrxb-1' ip='10.10.1.23'/>
      <host mac='0A:AA:FF:C1:B2:EE' name='vsrxb-2' ip='10.10.1.24'/>
      <host mac='0A:AA:FF:C1:C1:EE' name='vsrxc-1' ip='10.10.1.25'/>
      <host mac='0A:AA:FF:C1:C2:EE' name='vsrxc-2' ip='10.10.1.26'/>
      <host mac='0A:AA:FF:C1:D1:EE' name='vsrxd-1' ip='10.10.1.27'/>
      <host mac='0A:AA:FF:C1:D2:EE' name='vsrxd-2' ip='10.10.1.28'/>
    </dhcp>
  </ip>
</network>
strings="      STRING1...\n      STRING2...\n      ...\n" 
match="<dhcp>"  
awk -v m="$match" -v s="$strings" '{ print; if($1 == m) printf s }' input_file

<network>
  <name>br-ext</name>
  <forward mode='route'/>
  <bridge name='br-ext' stp='on' delay='0'/>
  <mac address='52:54:00:9f:a0:00'/>
  <ip address='10.10.1.11' netmask='255.255.255.0'>
    <dhcp>
      STRING1...
      STRING2...
      ...
      <host mac='0A:AA:FF:C1:A1:EE' name='vsrxa-1' ip='10.10.1.21'/>
      <host mac='0A:AA:FF:C1:A2:EE' name='vsrxa-2' ip='10.10.1.22'/>
      <host mac='0A:AA:FF:C1:B1:EE' name='vsrxb-1' ip='10.10.1.23'/>
      <host mac='0A:AA:FF:C1:B2:EE' name='vsrxb-2' ip='10.10.1.24'/>
      <host mac='0A:AA:FF:C1:C1:EE' name='vsrxc-1' ip='10.10.1.25'/>
      <host mac='0A:AA:FF:C1:C2:EE' name='vsrxc-2' ip='10.10.1.26'/>
      <host mac='0A:AA:FF:C1:D1:EE' name='vsrxd-1' ip='10.10.1.27'/>
      <host mac='0A:AA:FF:C1:D2:EE' name='vsrxd-2' ip='10.10.1.28'/>
    </dhcp>
  </ip>
</network>
寂寞花火° 2025-02-02 19:19:12

对于类似的任务,我使用xmlstarlet。显然,您从XML源获得$ String。因此,您可以使用XMLSTARLET获取MAC,名称和IP值。假设你得到了它们。然后,您可以将新主机添加到$ file_path之类的类似:

xmlstarlet edit --inplace \
  --insert /network/ip/dhcp/host[1] --type elem --name new_host --value "" \
  --insert //new_host --type attr --name mac  --value $new_host_mac \
  --insert //new_host --type attr --name name --value $new_host_name \
  --insert //new_host --type attr --name ip   --value $new_host_ip \
  --rename //new_host --value host \
  $file_path

For a similar task I use xmlstarlet. Obviously you got $string from a xml source. Thus you can use xmlstarlet to get mac, name and ip values. Let's assume you got them. You can then add a new host into $file_path like this:

xmlstarlet edit --inplace \
  --insert /network/ip/dhcp/host[1] --type elem --name new_host --value "" \
  --insert //new_host --type attr --name mac  --value $new_host_mac \
  --insert //new_host --type attr --name name --value $new_host_name \
  --insert //new_host --type attr --name ip   --value $new_host_ip \
  --rename //new_host --value host \
  $file_path
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文