本文介绍了在bash特定子字符串后提取令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个bash脚本包含几行的字符串变量:
Suppose I have a string variable in a bash script that contained several lines:
blah blah blah ... ... an interesting parameter: 12345 some other useless stuff... ... ...我想从这个字符串中提取12345。我试图寻求新的方式使用'一个有趣的参数:'作为一个分隔符,但我不能完全得到它的工作。是否有这样做的一个干净的方式?
I want to extract 12345 from this string. I have tried to look for ways to use 'an interesting parameter:' as a "delimeter" but I couldn't quite get it to work. Is there a clean way of doing this?
推荐答案庆典支持正前pression匹配,而无需使用外部程序。
bash supports regular expression matching without using external programs.
$ str=' blah blah blah ... ... an interesting parameter: 12345 some other useless stuff... ... ...' $ [[ $str =~ an\ interesting\ parameter:\ ([[:digit:]]+) ]] $ echo ${BASH_REMATCH[1]} 12345阵列 BASH_REMATCH 包含元素0和捕获的分组全场比赛(左到右的顺序)在随后的元素。
The array BASH_REMATCH contains the full match in element 0 and the captured subgroups (in left-to-right order) in subsequent elements.
更多推荐
在bash特定子字符串后提取令牌
发布评论