【发布时间】:2021-12-29 10:20:37
【问题描述】:
我正在使用 centos 7. sed 命令替换 second 事件对我不起作用。 我尝试了以下解决方案 - Sed replace at second occurrence Sed/Awk to delete second occurence of string - platform independent sed: How to delete second match in a file https://unix.stackexchange.com/questions/18303/sed-delete-all-occurrences-of-a-string-except-the-first-one
文件 -
this
foo
is
a test
file
this
foo
我正在尝试运行 -
sed 's/foo/bar/2g' file
sed 's/foo/bar/2' file
sed -i 's/foo/bar/2g' file
我希望用第二个中的“bar”替换所有出现的“foo”。
【问题讨论】:
-
您试图模拟的答案旨在替换模式在每一行中的第二次出现。它不包含用于跟踪多行模式出现的机制或逻辑。
-
您想只替换整个文件中的第二个外观吗?或者,也许每个偶数出现?还是第二次和所有随后的出场?你能假设至少会出现两次吗?你能假设正好有两个吗?