bash - 从字符串中第二次出现的破折号中提取字符
问题描述
我有一个字符串“ apple-rose-orange-21-X-84 ”;我需要使用 shell 脚本在第二次出现“ - ”之后提取字符串。
答案应该是orange-21-X-84。
我将如何在 shell 脚本中执行此操作,我尝试了多种方法,但找不到答案。
解决方案
shell only solution:
string='apple-rose-orange-21-X-84'
echo ${string#*-*-}
${parameter#word}
${parameter##word}
The word is expanded to produce a pattern and matched according to the rules described below (see Pattern Matching). If the pattern matches the beginning of the expanded value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the ‘#’ case) or the longest matching pattern (the ‘##’ case) deleted. If parameter is ‘@’ or ‘<em>’, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with ‘@’ or ‘</em>’, the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.
推荐阅读
- c++ - 是否有 C++ 习语通过模板魔术将所谓的速率组(实时计算)编码为函数?
- gitlab - 在发布合并请求之前运行 gitlab 管道
- c++ - 从函数内部定义的结构访问函数的内容
- python - 如何检测用户是否单击了 tkinter 按钮?
- c++ - 另一个容器内的c ++容器
- machine-learning - Julia 中的 GPU 处理,改动很小
- c# - 使用 EF 生成的 MySQL 在 LinQ 中为 FromSqlRaw 在从系统中选择变量时返回“SQL 语法错误”
- python - 创建密码解码器的反转部分
- django - Django 无法在下一个模板中传递变量名
- swift - Swift 活动指示器仅在某些时候出现