简体   繁体   English

sed用file1中的file3内容替换file2内容

[英]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.

相关问题 使用sed和grep在file1中查找标题,并将内容移动到具有相同标题的file2 - use sed and grep to find title in file1 and move the content to file2 with same title shell:通过FILE2中的内容从FILE1中获取行 - shell: Get line from FILE1 by content in FILE2 如何使用 sed 或 awk 将 file1 的一部分替换为 file2 的一部分,部分由行和列描述 - How to replace a portion of file1 by a portion of file2 uing sed or awk, portion delineated by row and column no 如何从从 file1 查询到 file2 的匹配结果中编写具有不同列的 file3? - How can I write a file3 with different columns from a matched result queried from file1 to file2? 如果在文件 2 中找不到字符串,则用空格替换文件 1 中的字符串 - replace strings in file1 with empty space if strings not found in file2 是否可以使用 bash cmd 获取 file1 减去 file2 的内容? - is it possible to get the content of file1 minus file2 by using bash cmd? awk查找并在匹配时将变量file2替换为file1 - awk find and replace variable file2 into file1 when matched 在基于file1的file2中搜索字符串并替换 - Search for a string in file2 based on file1 and replace 如果file1的第一列与file2中的任何字符串匹配,则将其替换为file1的第二列 - If the first column of file1 matches any string in file2, then replace it with the second column of file1 需要用file1的第二列替换与file1的第一列匹配的file2中的字符串 - Need to replace string in file2 that matches first column of file1 with second column of file1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM