如何在Shell脚本中修剪字符串

时间:2020-06-11 19:38:24

标签: shell

我有一个字符串

var=refs/heads/testing/branch

我想使用shell脚本摆脱字符串中的refs/heads/,这样我就只有:

var=testing/branch

我尝试过的命令(每行一个):

echo $(var) | awk -F\\ {'print $2'} 
echo $var | sed -e s,refs/heads/,, 
echo "refs/heads/testing/branch" | grep -oP '(?<=refs/heads/\)\w+' 
echo "refs/heads/testing/branch" | LC_ALL=C sed -e 's/.*\\//' 
echo "refs/heads/testing/branch" | cut -d'\' -f2 
echo refs/heads/testing/branch | sed -e s,refs/heads/,, 

2 个答案:

答案 0 :(得分:0)

有很多选择,尝试简单的选择:

echo $var | cut -d "/"  -f 3,4
echo $var | awk -F"/" '{print $3"/"$4}'

答案 1 :(得分:0)

Shell参数扩展:从变量内容中删除前缀“ refs / heads /”

$ var=refs/heads/testing/branch
$ echo "${var#refs/heads/}"
testing/branch
相关问题