gpt4 book ai didi

regex - 如何使用正则表达式在列表中每个匹配项的第一次出现周围添加 `\macro{}`

转载 作者:行者123 更新时间:2023-12-02 21:57:28 25 4
gpt4 key购买 nike

我有一个单词列表,list.txt,如下所示:

fish
squirrel
bird
tree
mountain

我还有一个文件,text.txt,其中包含如下段落:

The fish ate the birds.
The squirrel lived in the tree on the mountain.
The fish did not like eating squirrels as they lived too high in the trees.

我需要使用 TeX 代码标记 text.txt 文件中 list.txt 中所有单词的第一次出现,例如 \macro{},例如,输出如下所示:

The \macro{fish} ate the \macro{bird}s.
The \macro{squirrel} lived in the \macro{tree}house on the \macro{mountain}.
The fish did not like eating squirrels as they lived too high in the trees.

如何将 \macro{} 添加到 BASH 列表中出现的每个单词的第一次出现位置?

最佳答案

GNU 代码 :

$ sed -nr 's#(\w+)#s/\1/\1/;T\1;x;s/\1/\1/;x;t\1;x;s/.*/\& \1/;x;s/\1/\\\\macro\{\1\}/;:\1;$!N#p' list.txt|sed -rf - text.txt
$ cat list.txtfishsquirrelbirdtreemountain$ cat text.txtThe fish ate the birds.The squirrel lived in the tree on the mountain.The fish did not like eating squirrels as they lived too high in the trees.$ sed -nr 's#(\w+)#s/\1/\1/;T\1;x;s/\1/\1/;x;t\1;x;s/.*/\& \1/;x;s/\1/\\\\macro\{\1\}/;:\1;$!N#p' list.txt|sed -rf - text.txtThe \macro{fish} ate the \macro{bird}s.The \macro{squirrel} lived in the \macro{tree} on the \macro{mountain}.The fish did not like eating squirrels as they lived too high in the trees.

关于regex - 如何使用正则表达式在列表中每个匹配项的第一次出现周围添加 `\macro{}`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17509983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com