sed - 添加具有匹配组值的新行

sed - add new line with matched groups value

我输入的文本带有模式'([\w_]+TAG) = "\w+";',如果匹配,则附加一个包含匹配组字符串的新行,如 'found '。例如:

输入文字:

abcTAG = "hello";
efgTAG = "world";

预期输出:

abcTAG = "hello";
found abcTAG
efgTAG = "world";
found efgTAG

我尝试了下面的 sed 命令但没有用:

sed -E '/(\w+TAG) = "\w+";/a found ' a.txt

当前输出:

abcTAG = "hello";
found 1
efgTAG = "world";
found 1

您不能在 a 命令中使用反向引用 </code>。请改为尝试:</p> <pre><code>sed -E 's/(\w+TAG) = "\w+";/&\nfound /' a.txt

输出:

abcTAG = "hello";
found abcTAG
efgTAG = "world";
found efgTAG

请注意它假设 GNU sed 支持 \w\n

[编辑]
如果你想匹配输入文件的行尾,请尝试:

sed -E 's/(\w+TAG) = "\w+";(\r?)/&\nfound /' a.txt