简体   繁体   English

如何遍历两个文件并逐行查找file1中匹配file2的所有匹配项,然后替换为file3中的内容

[英]How to iterate over two files and find all occurrences in file1 matching file2, line by line, then replace with content from file3

I have a file I am trying to initiate a global find and replace pattern.我有一个文件,我正在尝试启动全局查找和替换模式。 I want to find all occurrences of file2, line by line, within file1, then replace these occurrences with file3 which match the pattern of file2.我想在file1中逐行查找所有出现的file2,然后用匹配file2模式的file3替换这些出现。

This is the file I am trying to chage.这是我要更改的文件。

file1.txt:
  34   │ ![[Pasted image 20220506211935.png]]
  35   │ 
  36   │ ![[Pasted image 20220506212047.png]]
  37   │ 
  38   │ ![[Pasted image 20220506212121.png]]
  39   │ 
  40   │ ![[Pasted image 20220506213028.png]]

I would like to search file1 with each pattern in this file line by line.我想用这个文件中的每个模式逐行搜索 file1 。

file2.txt:
   1   │ 20220506211935.png
   2   │ 20220506212047.png
   3   │ 20220506212121.png
   4   │ 20220506213028.png

Each search pattern from file2 would then be replaced with the content from file3.然后将 file2 中的每个搜索模式替换为 file3 中的内容。

file3.txt:
   1   │ ![image](https://m0d1cumc0rvu5.github.io/docs/assets/images/20220506211935.png)
   2   │ ![image](https://m0d1cumc0rvu5.github.io/docs/assets/images/20220506212047.png)
   3   │ ![image](https://m0d1cumc0rvu5.github.io/docs/assets/images/20220506212121.png)
   4   │ ![image](https://m0d1cumc0rvu5.github.io/docs/assets/images/20220506213028.png)

I have failed with vim:我用 vim 失败了:

%s#![[Pasted image #![image](https://m0d1cumc0rvu5.github.io/docs/assets/images/#g
%s#]]#)#g

This does not work in the .md file I am targeting.这在我定位的 .md 文件中不起作用。 (file1.txt in example) (例如file1.txt)

Similarly, using sed directly on the file with the current syntax does not work.同样,直接在具有当前语法的文件上使用 sed 也不起作用。

sed -r 's#[[Pasted image #![image](https://m0d1cumc0rvu5.github.io/docs/assets/images/#g' file1.txt

This is why I created the additional files to ideally loop over them such as:这就是为什么我创建了附加文件以理想地循环它们,例如:

sed -i 's/`cat file2.txt`/`cat file3.txt/g' file1.txt

I have seen similar questions, yet none have adequately answered what I am trying to achieve.我见过类似的问题,但没有人能充分回答我想要实现的目标。

Desired result:期望的结果:

file1.txt
  34   │ ![image](https://m0d1cumc0rvu5.github.io/docs/assets/images/20220506211935.png)
  35   │ 
  36   │ ![image](https://m0d1cumc0rvu5.github.io/docs/assets/images/20220506212047.png)
  37   │ 
  38   │ ![image](https://m0d1cumc0rvu5.github.io/docs/assets/images/20220506212121.png)
  39   │ 
  40   │ ![image](https://m0d1cumc0rvu5.github.io/docs/assets/images/20220506213028.png)
awk '
    # keep track of which file is being processed
    FNR==1 { file++ }

    # store search and replacement text
    file==1 { search[++i] = $0; next }
    file==2 { replace[++j] = $0; next }

    # sanity check
    file==3 && FNR==1 {
        if (i!=j) {
            print "search/replace mismatch" >"/dev/stderr"
            exit 1
        }
    }

    # look for lines to replace
    file==3 {
        for (k=1; k<=i; k++) {
            if (index($0,search[k])) {
                # found: print replacement and skip to next line
                print replace[k]
                next
            }
        }
        # no match: print original line
        print
   }
' file2.txt file3.txt file1.txt > new-file1.txt

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果在file2中找不到file1的输出行 - Output line from file1 if not found in file2 如何从从 file1 查询到 file2 的匹配结果中编写具有不同列的 file3? - How can I write a file3 with different columns from a matched result queried from file1 to file2? 如何测试 File2 中是否存在来自 File1 每一行的 substring - How to test if a substring from each line of File1 exists in File2 我想从 file1 中的第 1 列和第 2 列中找到一些与 file2 中的第 1 列匹配的字符串/单词,并替换为 file2 中的第 2 列字符串/单词 - I want to find some strings/words from column 1 and 2 in file1 that match column 1 in file2 and replace with column 2 strings/words in file2 在 Linux 命令行中,如何检查 file1 的内容是否存在于 file2 中? 然后找出file2中哪里存在类似的模式? - in the Linux command line, how do i check if the contents of a file1 exist in file2? Then find out where the similar patterns exist in file2? 如何基于文件/ file1(仅)第一列与linux中的file2的匹配信息从file1提取行? - how to extract rows from file1 based on matching information of its/file1 (only)first column with file2 in linux? 将带有一列的 file1 与来自 file2 的两列进行比较 - Compare file1 with one column to two columns from file2 合并两个文件,将 file1 中的一个字段替换为 file2 中的另一个字段 - Combine two files, replacing one field from file1 with another from file2 Linux 中两个文件的重叠或将 file1 中的行分别替换为 file2 中的行 - Overlap of two files in Linux or respective replacement of lines from file1 with lines from file2 使用 sed 或任何其他命令将部分行从 file1 复制到 file2 中的特定位置 - copy a part of a line from file1 to specific place in file2 using sed or any other command
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM