Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我的输出如下所示:
foo-1_2_3_4_5_bar foo-6_7_8_9_0_1_2_bar
如何删除尾随字符以便获得以下输出:
foo-1 foo-6
我试过:
awk 'sub("..........$", "")
但这非常不方便,因为字符串可以有不同的长度。
如果您的字符串在variable然后:
variable
echo ${variable%%_*}
有关bash内置字符串操作的附加信息
bash
sed 's/_.*//'
测试:
kent$ echo "foo-1_2_3_4_5_bar foo-6_7_8_9_0_1_2_bar"|sed 's/_.*//' foo-1 foo-6
添加一个 awk 单行:
awk -F_ '$0=$1'
相同的输入,相同的输出:
kent$ echo "foo-1_2_3_4_5_bar foo-6_7_8_9_0_1_2_bar"|awk -F_ '$0=$1' foo-1 foo-6