在第一次出现的定界符处拆分 string/variable (UNIX)
Split string/variable on first occurrence delimiter (UNIX)
我想用定界符分割字符串。
变量 my_string 包含 Hello_This_Is_My_string
输出将仅为
下面是我的代码:
result = $(echo $my_string |" cut -d '_' -f2")
但是,我得到的是 <Is>
而不是 <This_Is_My_string>
我找到了答案:
result=$(echo $my_string | cut -d "_" -f 2-)
测试:
echo aa_bb_cc | cut -d "_" -f 2-
避免调用外部命令,使用shell-internal 'Parameter Expansion',它有非常强大的选项
my_string='Hello_This_Is_My_string'
echo "result = ${my_string#*_}"
我想用定界符分割字符串。 变量 my_string 包含 Hello_This_Is_My_string 输出将仅为
下面是我的代码:
result = $(echo $my_string |" cut -d '_' -f2")
但是,我得到的是 <Is>
而不是 <This_Is_My_string>
我找到了答案:
result=$(echo $my_string | cut -d "_" -f 2-)
测试:
echo aa_bb_cc | cut -d "_" -f 2-
避免调用外部命令,使用shell-internal 'Parameter Expansion',它有非常强大的选项
my_string='Hello_This_Is_My_string'
echo "result = ${my_string#*_}"