Issue
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
Solution
All you need is 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
Answered By - George Vasiliou Answer Checked By - Terry (WPSolving Volunteer)