如何解决将每个第 n 次出现的 'foo' 替换为所提供文件的第 n 行数字
我在发布此问题之前寻找了解决方案,但我只找到了中间答案,例如 this 。我觉得很可能有和我完全一样或类似的问题,但我没有发现这样的问题。
我想用 0.txt 文件的第 n 个数字对应行替换 1.txt 文件中出现的每个 foo
以下内容(这是 MWE)。
0.txt:
The sun has its own light
foo
The moon reflects the sunlight
foo
The planet Earth receives both sunlight and moonlight
foo
1.txt:
cat1.frog1_rain1 (('f1','b1'),('g1','h1'))
cat2.frog2_rain2 (('f2','b2'),('g2','h2'))
cat3.frog3_rain3 (('f3','b3'),('g3','h3'))
应用替换,例如'command_method' 0.txt 1.txt > 2.txt
(伪代码),我想要的输出文件如下,是第三个 2.txt 文件上的打印输出:
2.txt:
The sun has its own light
cat1.frog1_rain1 (('f1','h1'))
The moon reflects the sunlight
cat2.frog2_rain2 (('f2','h2'))
The planet Earth receives both sunlight and moonlight
cat3.frog3_rain3 (('f3','h3'))
解决方法
请您试试awk
解决方案:
awk 'NR==FNR {a[NR]=$0; next} /foo/{gsub("foo",a[++i])} 1' 1.txt 0.txt > 2.txt
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。