首页 > 解决方案 > 在bash中删除字符串的一部分

问题描述

我如何refs/heads/从铰孔部分中取出部分refs/heads/hotfix/sub_pag/105并返回hotfix/sub_page/105

标签: regexbash

解决方案


您可以使用cut

echo refs/heads/hotfix/sub_pag/105 | cut -d/ -f3-
  • -d指定分隔符,/在这种情况下
  • -f指定要保留的列(字段)。我们想要从第 3 列开始的所有内容。

或者,使用变量和参数替换:

x=refs/heads/hotfix/sub_pag/105
echo ${x#refs/heads/}
  • #从变量值的开头删除字符串。

推荐阅读