如何 "give priority" 某些模式而不是 sed 中的其他模式?

How to "give priority" to certain pattern instead of others in sed?

我有这个 sed 过滤器:

/.*[1-9][0-9][0-9] .*/{
        s/.*\([1-9][0-9][0-9] .*\)//
}

/.*[1-9][0-9] .*/{
        s/.*\([1-9][0-9] .*\)//
}

/.*[0-9] .*/{    # but this is always preferred/executed
        s/.*\([0-9] .*\)//
}

问题是前两个限制比较多,没有执行,因为最后第三个比较“厉害”,因为它包含了前两个。有没有办法让 sed 以“优先顺序”取前两个?喜欢

if the first matches
    do first things
elif the second matches
    do second things
elif the third matches
    do third things
if .. elif

sed 是一种简单的 GOTO 语言。研究 sed 中的 b: 命令。

/.*[1-9][0-9][0-9] .*/{
        s/.*\([1-9][0-9][0-9] .*\)//
         b END
}

/.*[1-9][0-9] .*/{
        s/.*\([1-9][0-9] .*\)//
        b END
}

/.*[0-9] .*/{    # but this is always preferred/executed
        s/.*\([0-9] .*\)//
}

: END

这可能适合您 (GNU sed):

sed -E 's/(^|[^0-9])([1-9][0-9]{,2}|[0-9]) .*/\n\n/;s/.*\n(.*)\n.*//' file

我假设您想捕获一个 1,2 或 3 位数字,后跟一个 space。

交替 | 从左到右。

以上正则表达式将捕获第一个匹配项或仅 return 整个字符串。

N.B。 ^|[^0-9] 是将匹配限制为 1,2 或 3 位数字所必需的。

如果所需字符串在一行中出现多次,则匹配项可能会更改为第 n 个匹配项,例如第二个:

sed -E 's/(^|[^0-9])([1-9][0-9]{,2}|[0-1]) .*/\n\n/2;s/.*\n(.*)\n.*//' file

上述情况的最后一场比赛是:

sed -E 's/(^|.*[^0-9])([1-9][0-9]{,2}|[0-1]) .*/\n\n/;s/.*\n(.*)\n.*//' file