bash 如何从bash中的字符串中获取最后一个数字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44089977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the last number from string in bash?
提问by Phocs
Sorry, I'll explain it better
对不起,我会解释得更好
How can I get the last number from string?
如何从字符串中获取最后一个数字?
Examples of generics strings:
泛型字符串的例子:
If str=str1s2
echo $str | cmd?
I get 2
If str=234ef85
echo $str | cmd?
I get 85
If str=djfs1d2.3
echo $str | cmd?
I get 3
"cmd?" is the command/script that I want
“命令?” 是我想要的命令/脚本
回答by George Vasiliou
All you need is grep -Eo '[0-9]+$'
:
您只需要 grep -Eo '[0-9]+$'
:
gv@debian:~$ echo 234ef85 |grep -Eo '[0-9]+$' ## --> 85
gv@debian:~$ echo 234ef856 |grep -Eo '[0-9]+$' ## --> 856
gv@debian:~$ echo 234ef85d6 |grep -Eo '[0-9]+$' ## --> 6
gv@debian:~$ echo 234ef85d.6 |grep -Eo '[0-9]+$' ## --> 6
gv@debian:~$ echo 234ef85d.6. |grep -Eo '[0-9]+$' ## --> no result
gv@debian:~$ echo 234ef85d.6.1 |grep -Eo '[0-9]+$' ## --> 1
gv@debian:~$ echo 234ef85d.6.1222 |grep -Eo '[0-9]+$' ## --> 1222
回答by choroba
You can use parameter expansion with extglob. First, remove the number from the end, then remove what you got from the beginning.
您可以通过 extglob 使用参数扩展。首先,从末尾删除数字,然后删除从开头得到的数字。
#!/bin/bash
shopt -s extglob
for str in str1s2 djfs1d2.3 fefwfw4rfe45 234ef8 ; do
without_number=${str%%+([0-9])}
echo ${str#$without_number}
done
回答by Walter A
You can use grep
with
你可以用grep
与
rev <<< "$str" | grep -Eo "[0-9]*" | head -1 |rev
EDIT:
rev
is not needed when I use tail -1
but the head/tail are overdone when you just add the end-of-line marker $
like @Vasiliou did (I upvoted his answer). Without rev
and head
the grep
solution is better than sed
.
I deleted my remark "Better is using sed
".
编辑:
rev
当我使用时不需要,tail -1
但是当您$
像@Vasiliou 那样添加行尾标记时,头部/尾部过度了(我赞成他的回答)。如果没有rev
与head
该grep
解决方案比更好sed
。我删除了我的评论“更好的是使用sed
”。
sed -r 's/.*[^0-9]+([0-9]*)$//' <<< "$str"
回答by PSkocik
With awk:
使用 awk:
INPUT:
输入:
str1 = "str1s2"
str2 = "djfs1d2.3"
str3 = "fefwfw4rfe45"
str4 = "234ef8"
command:
命令:
tr = \ < INPUT |
awk '{ match(,"[0-9]*\"$");
printf "%s: %s\n", , substr(,RSTART,RLENGTH-1); }'
output:
输出:
str1: 2
str2: 3
str3: 45
str4: 8
回答by RomanPerekhrest
Short gawkapproach (for multiple variables):
简短的gawk方法(对于多个变量):
echo "$str1 $str2 $str3 $str4 " | awk -v FPAT="[0-9]+ " '{for(i=1;i<=NF;i++) print "str"i": "$i}'
The output:
输出:
str1: 2
str2: 3
str3: 45
str4: 8
FPAT="[0-9]+ "
- a regexp that matches the fields, instead of matching the field separator
FPAT="[0-9]+ "
- 匹配字段的正则表达式,而不是匹配字段分隔符
As you have changed your initial condition:
For one single string it would be even simpler:
由于您更改了初始条件:
对于单个字符串,它会更简单:
echo djfs1d2.3 | awk -v FPAT="[0-9]+" '{print $NF}'
3
回答by Vojtech Vitek
Get all numbers from a string:
从字符串中获取所有数字:
grep -Eo '[0-9]+'
Get last number from a string:
从字符串中获取最后一个数字:
grep -Eo '[0-9]+' | tail -1
(Expanding on George's answer a bit..)
(稍微扩展乔治的回答..)
-E
means extended regex-o
means print matching parts of the line
-E
意味着扩展正则表达式-o
表示打印该行的匹配部分