Operating System - HP-UX
1752577 Members
3992 Online
108788 Solutions
New Discussion

Re: get the last digit(s) from a line or file

 
SOLVED
Go to solution
support_billa
Valued Contributor

get the last digit(s) from a line or file

hello,

 

i tried several tests to get the last digit(s) from a line or file.

my prefered command is "sed"

 

but in "sed"   '*' doesn't   mean zero or more occurence like in "grep" , so i use "{0,\}"

 

example:

RETURN 1

RETURN 11

RETURN 1234

 

my tests :

echo "RETURN 1" | sed -n '$s/.* \([0-9]\{0,\}\)$/\1/p'
echo "RETURN 11" | sed -n '$s/.* \([0-9]\{0,\}\)$/\1/p'

regards

11 REPLIES 11
Dennis Handly
Acclaimed Contributor

Re: get the last digit(s) from a line or file

>but in "sed"  '*' doesn't  mean zero or more occurrence like in "grep" , so I use "{0,\}"

 

"*" works fine for me.

$ echo "RETURN 1234" | sed -n '$s/.* \([0-9]*\)$/\1/p'
1234

 

Steven Schweda
Honored Contributor

Re: get the last digit(s) from a line or file

 
Steven Schweda
Honored Contributor

Re: get the last digit(s) from a line or file

 
Dennis Handly
Acclaimed Contributor

Re: get the last digit(s) from a line or file

>Let's see if the attachment scheme still works.

 

Yes but it is very hard to get to your attachment.

And you might be able to delete your empty post if you hadn't replied to it.

James R. Ferguson
Acclaimed Contributor
Solution

Re: get the last digit(s) from a line or file

Hi:

 

# echo 'RETURN 1234' | sed -e 's/[^0-9]*\([0-9]*\)$/\1/'
1234
# echo 'RETURN1234'  | sed -e 's/[^0-9]*\([0-9]*\)$/\1/'
1234

 Regards!

 

...JRF...

support_billa
Valued Contributor

Re: get the last digit(s) from a line or file

hello,

 

thx for your perfect answers.

i have a solution

 

regards

 

Pete Randall
Outstanding Contributor

Re: get the last digit(s) from a line or file

How about a kudo to go with that solution flag?


Pete
James R. Ferguson
Acclaimed Contributor

Re: get the last digit(s) from a line or file


@Pete Randall wrote:

How about a kudo to go with that solution flag?


As Pete has pointed out, marking a solution does *not* creat a kudos.  That's another counter-intuitive new feature of this new-fangled community we share.

 

...JRF...

Steven Schweda
Honored Contributor

Re: get the last digit(s) from a line or file