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