shell 脚本从一个文件中读取行并将其 sed 到另一个文件中
shell script to read the line from one file and sed it in another file
我需要从一个文件中读取值并将该值存储到另一个文件中file.so这里是文件的内容
file1.txt
test1.example.com
test2.example.com
test3.example.com
file2.txt
### list of servers ##
[group]
server1 ansible_host=pub_ip1 ansible_user=eadmin
server2 ansible_host=pub_ip2 ansible_user=eadmin
server3 ansible_host=pub_ip3 ansible_user=eadmin
所以预期的输出应该低于
file2.txt
### list of servers ##
[group]
test1.example.com ansible_host=pub_ip1 ansible_user=eadmin
test2.example.com ansible_host=pub_ip2 ansible_user=eadmin
test3.example.com ansible_host=pub_ip3 ansible_user=eadmin
file1 的第一行应该替换 file2 中的 server1 等等。我对 bash 和 Linux 相当陌生。如果有人可以帮助我实现这一点,那就太好了。
提前致谢
这可能适合您 (GNU sed):
sed -e '/[group]/!b;:a;n;R file1' -e 'ba' file2 |
sed '1,/[group]/b;N;s/\n\S\+\s*/ /'
在第一次 sed 调用中将 file1 与 file2 交错。
将结果通过管道传递给第二个 sed 调用,用上面的行替换第一个字段以进行所需的替换。
我需要从一个文件中读取值并将该值存储到另一个文件中file.so这里是文件的内容
file1.txt
test1.example.com
test2.example.com
test3.example.com
file2.txt
### list of servers ##
[group]
server1 ansible_host=pub_ip1 ansible_user=eadmin
server2 ansible_host=pub_ip2 ansible_user=eadmin
server3 ansible_host=pub_ip3 ansible_user=eadmin
所以预期的输出应该低于
file2.txt
### list of servers ##
[group]
test1.example.com ansible_host=pub_ip1 ansible_user=eadmin
test2.example.com ansible_host=pub_ip2 ansible_user=eadmin
test3.example.com ansible_host=pub_ip3 ansible_user=eadmin
file1 的第一行应该替换 file2 中的 server1 等等。我对 bash 和 Linux 相当陌生。如果有人可以帮助我实现这一点,那就太好了。
提前致谢
这可能适合您 (GNU sed):
sed -e '/[group]/!b;:a;n;R file1' -e 'ba' file2 |
sed '1,/[group]/b;N;s/\n\S\+\s*/ /'
在第一次 sed 调用中将 file1 与 file2 交错。
将结果通过管道传递给第二个 sed 调用,用上面的行替换第一个字段以进行所需的替换。