如何将文件中的字符串添加到匹配文件中 [英] How to add a string from a file into matching files
问题描述
我正在尝试浏览工作站列表,并将数据添加到其相应的单个文件中.我已经制作了单个文件,但是现在我需要使用sed或其他方法来替换单个XML文件中的文本的帮助:
I am trying to go through a list of workstations, and add data to their corresponding individual files. I have already made the individual files, but now I need help using sed or something else to replace text in the individual XML files:
例如:
workstation_list.txt具有以下几行:
workstation_list.txt has the following lines:
workstation1
workstation1.domain.com
127.0.0.1
00:00:00:00:00:00
workstation2
workstation2.domain.com
127.0.0.2
11:11:11:11:11:11
我有两个文件:带有以下XML的工作站1和工作站2:
I have two files: workstation1 and workstation2 with the following XML:
< HOST_NAME >workstation3< /HOST_NAME >
< HOST_FQDN >workstation3.domain.com< /HOST_FQDN >
< IP >127.0.0.0< /IP >
< MAC >33:33:33:33:33:33< /MAC >
我可以做"while read line do",而不会出现问题,但是我从未使用过多个变量.
I can do a "while read line do" without a problem, but I've never used more than one variable.
谢谢您的帮助!
推荐答案
只需调用 gawk 的问题:
awk -v RS='\n\n' '{
print "< HOST_NAME >" $1 "< /HOST_NAME >" > $1
print "< HOST_FQDN >" $2 "< /HOST_FQDN >" > $1
print "< IP >" $3 "< /IP >" > $1
print "< MAC >" $4 "< /MAC >" > $1
close($1)
}' file
鉴于您的示例输入,它将创建两个文件,如下所示:
Given your sample input it creates two files as follows:
工作站1 :
< HOST_NAME >workstation1< /HOST_NAME >
< HOST_FQDN >workstation1.domain.com< /HOST_FQDN >
< IP >127.0.0.1< /IP >
< MAC >00:00:00:00:00:00< /MAC >
工作站2 :
< HOST_NAME >workstation2< /HOST_NAME >
< HOST_FQDN >workstation2.domain.com< /HOST_FQDN >
< IP >127.0.0.2< /IP >
< MAC >11:11:11:11:11:11< /MAC >
这篇关于如何将文件中的字符串添加到匹配文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!