bash - 如何只保留字符串中的特定字符

时间:2015-01-22 03:20:34

标签: string bash shell

在我的脚本中,我只需要保留字符串中的特定字符。 例如:

string="aPRO100z"

如何获得以下结果?

result="PRO100"

感谢您提供任何帮助。

2 个答案:

答案 0 :(得分:2)

您可以使用sed命令:

# in this example, we keep only the chars 0-9 and A-Z
result=`echo $string | sed 's/[^0-9A-Z]*//g'`;

答案 1 :(得分:0)

使用cut

result=$(echo ${string} | cut -c 2-7)