gpt4 book ai didi

linux - shell 中的 sed 命令不会替换模式的第二个匹配项

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:41:31 25 4
gpt4 key购买 nike

我有以下字符串,我想替换(删除)字符串中出现的第二个单词 (SECONDWORD)。以下行不会更改任何内容,只是打印相同的字符串。但是,当我删除字符串的“123”部分时,该行似乎有效。

echo "WORD 123 SECONDWORD THIRDWORD" | sed 's/ *[A-Z]* *//2'

我没有看到问题。

最佳答案

试试这个:sed -r 's/\b[A-Z]+ *//2

匹配零次出现的 * 是问题所在。

您可以通过播放尾随数字来了解这一点。当您有不匹配 [A-Z] 的空格分隔字符串时,您会在分隔不匹配字符串的空字符串上获得匹配项。

    [~/tmp] > echo 'WORD 123 SECONDWORD THIRDWORD FOURTHWORD' | sed 's/ *[A-Z]* *//1'    123 SECONDWORD THIRDWORD FOURTHWORD    [~/tmp] > echo 'WORD 123 SECONDWORD THIRDWORD FOURTHWORD' | sed 's/ *[A-Z]* *//2'    WORD 123 SECONDWORD THIRDWORD FOURTHWORD    [~/tmp] > echo 'WORD 123 SECONDWORD THIRDWORD FOURTHWORD' | sed 's/ *[A-Z]* *//3'    WORD 123 SECONDWORD THIRDWORD FOURTHWORD    [~/tmp] > echo 'WORD 123 SECONDWORD THIRDWORD FOURTHWORD' | sed 's/ *[A-Z]* *//4'    WORD 123THIRDWORD FOURTHWORD    [~/tmp] > echo 'WORD 123 SECONDWORD THIRDWORD FOURTHWORD' | sed 's/ *[A-Z]* *//5'    WORD 123 SECONDWORD FOURTHWORD

... so when you have 123 in there you actually want the 4th matching item removed. Where 'WORD ', null, null,' SECONDWORD ','THIRDWORD ' are matching patterns 1-5. It's matching two empty strings around the boundaries of 123.

You can fix this by using + and not * with sed -r:

[~/tmp] > echo 'WORD 123 SECONDWORD THIRDWORD FOURTHWORD' | sed -r 's/ *[A-Z]+ *//2'
WORD 123THIRDWORD FOURTHWORD

或者使用不带 -r 的更丑陋的 \{1,\} 语法:

[~/tmp] > echo 'WORD 123 SECONDWORD THIRDWORD FOURTHWORD' | sed 's/ *[A-Z]\{1,\} *//2'
WORD 123THIRDWORD FOURTHWORD

但是那个吃了一个你不想吃的空格,所以使用了\b字边界标记:

[~/tmp] > echo 'WORD 123 SECONDWORD THIRDWORD FOURTHWORD' | sed -r 's/\b[A-Z]+ *//2'
WORD 123 THIRDWORD FOURTHWORD

关于linux - shell 中的 sed 命令不会替换模式的第二个匹配项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44636721/

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