gpt4 book ai didi

linux - 从文件中的一组字符串中替换一个字符串 - SED

转载 作者:太空宇宙 更新时间:2023-11-04 10:13:39 24 4
gpt4 key购买 nike

我无法替换来自文件中的一组字符串的字符串所以这就是我希望我的脚本运行的方式

我有这些文件:macadd.文件

00:90:8F:56:63:88
00:90:8F:56:63:77
00:90:8F:56:63:6B
00:90:8F:56:63:86
00:90:8F:56:63:87
00:90:8F:56:64:E5

测试文件

host Accountant { hardware ethernet 00:90:8F:56:63:88; fixed-address 192.168.10.29; }
host Accountant { hardware ethernet 00:90:8F:56:63:88; fixed-address 192.168.10.29; }
host Accountant { hardware ethernet 00:90:8F:56:63:88; fixed-address 192.168.10.29; }
host Accountant { hardware ethernet 00:90:8F:56:63:88; fixed-address 192.168.10.29; }
host Accountant { hardware ethernet 00:90:8F:56:63:88; fixed-address 192.168.10.29; }
host Accountant { hardware ethernet 00:90:8F:56:63:88; fixed-address 192.168.10.29; }

所以我尝试用

替换它
for i in $(cat macadd); do sed -i "s/00:90:8F:56:63:88/$i/g" test;done

但是,它只是从 macadd.file 中的第一个字符串开始更改您可以看到 00:90:8F:56:63:88 是那里的所有条目。我希望发生的事情是这样的,结果:

host Accountant { hardware ethernet 00:90:8F:56:63:88; fixed-address 192.168.10.29; }
host Accountant { hardware ethernet 00:90:8F:56:63:77; fixed-address 192.168.10.29; }
host Accountant { hardware ethernet 00:90:8F:56:63:6B; fixed-address 192.168.10.29; }
host Accountant { hardware ethernet 00:90:8F:56:63:86; fixed-address 192.168.10.29; }
host Accountant { hardware ethernet 00:90:8F:56:63:87; fixed-address 192.168.10.29; }
host Accountant { hardware ethernet 00:90:8F:56:64:E5; fixed-address 192.168.10.29; }

最佳答案

实际上,对于您在问题中提出的示例,没有替换的内容会更加简单明了:

<macadd.file xargs -I{} printf "host Accountant { hardware ethernet %s; fixed-address 192.168.10.29; }\n" {}

即输入文件中的每一行调用 printf 仅替换 MAC 地址部分。当您有更多的 MAC 地址和更多的 IP 地址时,麻烦就来了。

关于linux - 从文件中的一组字符串中替换一个字符串 - SED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47456487/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com