按指定模式(逗号)剪切字符串

cut a string after a specified pattern (comma)

我想剪切一个字符串并在第一次出现逗号后将其分配给一个变量。

my_string="a,b,c,d,e,f"

预期输出:

output="b,c,d,e,f"

当我使用命令时

output=`echo $my_string | cut -d ',' f2

我只得到 b 作为输出。

您必须在您要查找的位置后添加减号(-)。

a=`echo $my_string|cut -d "," -f 2-`

echo $a
b,c,d,e,f

在 -f2 的末尾添加破折号“-”将输出字符串的其余部分。

$ echo "a,b,c,d,e,f,g"|cut -d, -f2-

b,c,d,e,f,g

用参数扩展代替cut:

$ my_string="a,b,c,d,e,f"
$ output="${my_string#*,}"
$ echo "$output"
b,c,d,e,f

${my_string#*,}代表"remove everything up to and including the first comma from my_string"(见Bash manual)。