我有一个这样的 AWK 脚本,我将在一个文件上运行它:
cat input.txt | awk 'gsub(/[^ ]*(fish|shark|whale)[^ ]*/,"(&)")' >> output.txt
这会为所有包含单词“fish”、“shark”或“whale”的行添加括号,例如:
The whale asked the shark to swim elsewhere.
The fish were unhappy.
通过脚本运行后,文件变为:
The (whale) asked the (shark) to swim elsewhere.
The (fish) were unhappy.
该文件标有 HTML 标签,我需要让替换只发生在<b>
和</b>
标签之间。
The whale asked <b>the shark to swim</b> elsewhere.
<b>The fish were</b> unhappy.
这变成:
The whale asked <b> the (shark) to swim </b> elsewhere.
<b> The (fish) were </b> unhappy.
- 匹配的粗体标签永远不会放在不同的行上。开始
<b>
标签总是与结束标签出现在同一行</b>
。
如何将awk
的搜索限制为仅搜索和修改在<b>
和</b>
标签之间找到的文本?