Regex 如何在shell中使用sed或grep在字符串的前两个破折号之间提取文本

Regex 如何在shell中使用sed或grep在字符串的前两个破折号之间提取文本,regex,shell,sed,makefile,grep,Regex,Shell,Sed,Makefile,Grep,我有这样的字符串feature/test-111-test-test。 我需要提取字符串直到第二个破折号,并将正斜杠更改为破折号 我必须在Makefile中使用shell语法来完成这项工作,但是对于我来说,一些正则表达式无法工作,这对解决这个问题很有帮助 最后,我必须像这样获得smth: 输入-特征/测试-111-测试-test 输出-功能测试-111-或至少功能测试-111 但是grep-oP在我的情况下不起作用。这个regexp也不能正常工作-.*?-.*?-....您可以使用awk,请记住

我有这样的字符串feature/test-111-test-test。 我需要提取字符串直到第二个破折号,并将正斜杠更改为破折号

我必须在Makefile中使用shell语法来完成这项工作,但是对于我来说,一些正则表达式无法工作,这对解决这个问题很有帮助

最后,我必须像这样获得smth: 输入-特征/测试-111-测试-test 输出-功能测试-111-或至少功能测试-111

但是grep-oP在我的情况下不起作用。这个regexp也不能正常工作-.*?-.*?-....

您可以使用awk,请记住在Makefile中的$char in awk命令:

url=$shell echo'feature/test-111-test-test'| awk-F'-''{gsub/\/,-,$$1;print$$1-$$2-}' echo$url =>功能测试-111- 看。此处,-F'-'将字段分隔符设置为-,gsub/\/,-,$1将字段1中的/替换为-并打印$1-$2-打印--分隔字段1和2的值

或者,使用正则表达式作为字段分隔符:

url=$shell echo'feature/test-111-test-test'| awk-F'[-/]'{print$$1-$$2-$$3-}' echo$url =>功能测试-111- -F'[-/]'选项将字段分隔符设置为-和/

{print$1-$2-$3-}部分使用分隔连字符打印第一个、第二个和第三个值

请参阅。

您可以使用awk,请记住在Makefile中的$char in awk命令:

url=$shell echo'feature/test-111-test-test'| awk-F'-''{gsub/\/,-,$$1;print$$1-$$2-}' echo$url =>功能测试-111- 看。此处,-F'-'将字段分隔符设置为-,gsub/\/,-,$1将字段1中的/替换为-并打印$1-$2-打印--分隔字段1和2的值

或者,使用正则表达式作为字段分隔符:

url=$shell echo'feature/test-111-test-test'| awk-F'[-/]'{print$$1-$$2-$$3-}' echo$url =>功能测试-111- -F'[-/]'选项将字段分隔符设置为-和/

{print$1-$2-$3-}部分使用分隔连字符打印第一个、第二个和第三个值

请参阅。

获取第n次出现的字符C,您不需要花哨的perl正则表达式。相反,构建一个非C的正则表达式,然后用C表示n次:

要获得第n次出现的字符C,不需要花哨的perl正则表达式。相反,构建一个非C的正则表达式,然后用C表示n次:

带着麻布和剪纸

输出

feature-test-111
feature-test-111-
输出

feature-test-111
feature-test-111-
带着麻布和剪纸

输出

feature-test-111
feature-test-111-
输出

feature-test-111
feature-test-111-
另一个使用捕获组和正则表达式/模式迭代的sed解决方案与Socowi使用的相同:

$ s='feature/test-111-test-test'
$ sed -E 's/\//-/;s/^(([^-]*-){3}).*$/\1/' <<< "${s}"
feature-test-111-
另一个使用捕获组和正则表达式/模式迭代的sed解决方案与Socowi使用的相同:

$ s='feature/test-111-test-test'
$ sed -E 's/\//-/;s/^(([^-]*-){3}).*$/\1/' <<< "${s}"
feature-test-111-

您可以使用简单的BRE正则表达式形式not something-then the something,即[^-]*-来获取除-a-之外的所有字符

这项工作:

echo 'feature/test-111-test-test' | sed -nE 's/^([^/]*)\/([^-]*-[^-]*-).*/\1-\2/p'
feature-test-111-

您可以使用简单的BRE正则表达式形式not something-then the something,即[^-]*-来获取除-a-之外的所有字符

这项工作:

echo 'feature/test-111-test-test' | sed -nE 's/^([^/]*)\/([^-]*-[^-]*-).*/\1-\2/p'
feature-test-111-

使用参数展开/替换的另一个想法:

s='feature/test-111-test-test'
tail="${s//\//-}"                   # replace '/' with '-'

# split first field from rest of fields ('-' delimited); do this 3x times

head="${tail%%-*}"                  # pull first field
tail="${tail#*-}"                   # drop first field

head="${head}-${tail%%-*}"          # pull first field; append to previous field
tail="${tail#*-}"                   # drop first field

head="${head}-${tail%%-*}-"         # pull first field; append to previous fields; add trailing '-'

$ echo "${head}"
feature-test-111-

使用参数展开/替换的另一个想法:

s='feature/test-111-test-test'
tail="${s//\//-}"                   # replace '/' with '-'

# split first field from rest of fields ('-' delimited); do this 3x times

head="${tail%%-*}"                  # pull first field
tail="${tail#*-}"                   # drop first field

head="${head}-${tail%%-*}"          # pull first field; append to previous field
tail="${tail#*-}"                   # drop first field

head="${head}-${tail%%-*}-"         # pull first field; append to previous fields; add trailing '-'

$ echo "${head}"
feature-test-111-
没有扩展正则表达式的简短sed解决方案:

sed 's|\(.*\)/\([^-]*-[^-]*\).*|\1-\2|'
没有扩展正则表达式的简短sed解决方案:

sed 's|\(.*\)/\([^-]*-[^-]*\).*|\1-\2|'

grep-oP不起作用。你确定是格雷普没用吗。您发布的命令似乎已损坏,第一部分只是一个字符串。我希望像echo这样的东西能打印出那个字符串。最后还有一个流浪者。grep-oP不起作用。你确定是格雷普没用吗。您发布的命令似乎已损坏,第一部分只是一个字符串。我希望像echo这样的东西能打印出那个字符串。最后还有一个流浪汉。谢谢回复@wiktor stribiżew。对我来说,它不能正常工作。s='feature/test-111-test-test'url=$shell awk-F'[-/]'{print$1-$2-$3-}'@vitalisotnichenko在Makefile中,$。你需要加倍它,所以使用url=$shell awk-F'[-/]'{print$$1-$$2-$$3-}'@vitaliIstonichenko现在就可以运行我的更新解决方案了吗?@vitaliIstonichenko你可以在print命令中附加http://awk-F'-''{gsub/\/\/,-,$$1;print http://$$1-$$2-}'或awk-F'[-/]'{print http://$$1-$$2-/$$3-}对我来说非常有效,非常感谢您的回复@wiktor stribiżew。对我来说,它不能正常工作。s='feature/test-111-test-test'url=$shell awk-F'[-/]'{print$1-$2-$3-}'@vitalisotnichenko在Makefile中,$。你需要加倍它,所以使用url=$shell awk-F'[-/]'{print$$1-$$2-$$3-}'@vitaliIstonichenko现在就可以运行我的更新解决方案了吗?@vitaliIstonichenko你可以在print命令中附加http://awk-F'-''{gsub/\/\/,-,$$1;print http://$$1-$$2-}'或awk-F'[-/]'{print http://$$1-$$2-/$$3-}非常适合我,谢谢你