[英]sed replace file2 content with file3 content in file1
H ow to search file1 for file2 contents and replace them with file3 contents using sed? ^ h流量来搜索文件1的文件2页的内容,并使用sed的file3的内容替换它们?
file1 "multiline"文件1“多行”
<DB>
<Person><Name>Zack</Name>
<PersonData>...</PersonData>
</Person>
<Person><Name>Dave</Name>
<PersonData>...</PersonData>
</Person>
<Person><Name>Lisa</Name>
<PersonData>...</PersonData>
</Person>
<Person><Name>Zack</Name>
<PersonData>...</PersonData>
</Person>
</DB>
file2 "multiline"文件2“多行”
<Person><Name>Zack</Name>
<PersonData>...</PersonData>
</Person>
file3 "multiline"文件3“多行”
<Employee><Name>Zack</Name>
<EmployeeData>...</EmployeeData>
</Employee>
I'm trying to replace every Zack person with an employee.我正在尝试用一名员工替换每个 Zack 人。 I've tried from simple
我已经尝试过从简单
sed -i -r -e '1h;2,$H;$!d;g' -e "/PLACEHOLDER/ r $2" -e "s/PLACEHOLDER// $3" $1
to even more without any successful sed operation in order to change file1 to:在没有任何成功的 sed 操作的情况下,为了将 file1 更改为:
<DB>
<Employee><Name>Zack</Name>
<EmployeeData>...</EmployeeData>
</Employee>
<Person><Name>Dave</Name>
<PersonData>...</PersonData>
</Person>
<Person><Name>Lisa</Name>
<PersonData>...</PersonData>
</Person>
<Employee><Name>Zack</Name>
<EmployeeData>...</EmployeeData>
</Employee>
</DB>
Edit #1: Employee xml tags are completely different from person tags.编辑 #1:员工 xml 标签与人员标签完全不同。
Edit #2: I'm not parsing xml, am trying to replace occurrences only through sed or awk.编辑 #2:我不是在解析 xml,而是试图仅通过 sed 或 awk 替换出现的内容。
This might work for you (GNU sed):这可能对你有用(GNU sed):
sed -z 's/\n/\\n/g;1s/.*/s#&#/;2s/.*/&#g/' file2 file3 | sed -z -f - file1
Create a substitution command from the match and replacement files (first quoting all newlines).从匹配和替换文件创建一个替换命令(首先引用所有换行符)。 Then apply the command against the source file.
然后对源文件应用该命令。
Consider also this bash solution:还要考虑这个 bash 解决方案:
$ cat file2
Name:John Due
Gender:Male
Age:21
Address: Texas
Name:Ed Mundo
Gender:Male
Age:41
Address: California
$ cat file22
Name:Ed Mundo
Gender:Male
Age:41
Address: California
$ cat file222
SurName:Ed Mundo
Gender:Male
Age:61
Address: Chicago
$ a="$(<file22)";b="$(<file222)";c="$(<file2)"
$ echo "${c//$a/$b}"
Name:John Due
Gender:Male
Age:21
Address: Texas
SurName:Ed Mundo
Gender:Male
Age:61
Address: Chicago
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.