find the last digits of a string

print out 201 in following string, Please note the chars before 201 are random, no fixed format.

ua07app201

what have you tried?

code removed

Becasue the chars before 201 are random, and it seems sed back reference can only match a fixed pattern, so sed back reference doesn't work for me. I have to end up using perl. But i would like to know the solution in shell.

$ echo ua07app201 | perl -ne ' $_ =~m /(\d+$)/; print $1'

201

echo "ua07app201"|awk -F "" '{print $(NF-2)$(NF-1)$NF}'

have you tried it?

# echo ua07app201 | sed 's/.*[^0-9]\(.*\)$/\1/'
201

echo 'ua07app201' | sed 's/.*[^0-9]//'
expr ua07app201 : '.*[^0-9]\([0-9]*\)$'
echo 'ua07app201' | nawk -F'[^0-9]' '{print $NF}'

Another one with Perl :slight_smile:

echo ua07app201|perl -nle'print/(\d+)$/'

i tried it. u can check also

I tried it with this result:

awk: The field -1 must be in the range 0 to 199.

 The input line number is 1.
 The source line number is 1

.

echo ua07app201 | sed "s/.*\(...$\)/\1/"

?

% MY_VAL=ua07app201
% echo ${MY_VAL+201}
201
% MY_VAL=ua07app202 
% echo ${MY_VAL+201}
201
#!/bin/ksh

a='ua07app201'
echo "${a##*[!0-9]}"

yes It works !!!!!!!!!

Can someone tell me the relevance of (.*\)

Matches 0 or more characters.

Isn't .* is also the same so why () added to it ?