简体   繁体   English

如何在Shell脚本中删除从特定索引字符开始的所有字符

[英]How to remove all characters starting from a specific index character in Shell scripting

K="Google Chrome 75.0.3770.100"
echo ${K//[a-zA-Z]/}

Output 产量


75.0.3770.100

Expected Output 预期产量


75

Need to remove all the characters starting from a specific index character; 需要删除所有从特定索引字符开始的字符; say . .

You can use extglob to get this done in single step: 您可以使用extglob一步完成此操作:

shopt -s extglob

K="Google Chrome 75.0.3770.100"
echo "${K//@([a-zA-Z ]|.*)/}"

75

Expression Details: 表达式详细信息:

  • @(...) : Match one of the expressions inside (...) separated by | @(...) :匹配(...)内由|分隔的表达式之一
  • [a-zA-Z ] : Match [a-zA-Z] letters or space [a-zA-Z ] :匹配[a-zA-Z]字母或空格
  • | : or : 要么
  • .* : Any string starting from a dot .* :从点开始的任何字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM