Operating System - HP-UX
1826402 Members
4056 Online
109692 Solutions
New Discussion

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

 
Pete Randall
Outstanding Contributor

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

Just for that Steven, I'm going back through every post I can find and taking back any kudos I've ever given you.  Take that!


Pete
Dennis Handly
Acclaimed Contributor

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

>"Very hard" how?  Clicking on it works for me

I would have to open a new tab and then refer back to the original.  You're making it hard for others to enjoy your wit and repartee.  ;-)
Perhaps you should enable and try the Quick Reply?

 

And to reply to you, I need to paste your attachment into notepad and then paste back.

(My Copy Plain Text isn't working with firefox 5.0.)

 

>I thought that it provided a nice reminder of why the next posting includes an annoying text attachment, 

 

Also if threading of replies is important to you, you should click on the reply below the post and not the one at the top.

(Disabled checking for Copy Plain Text and I didn't need to use notepad this time.)