Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Select the last two characters from a line of output

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
pc



Joined: Fri Apr 08, 2005 6:12 pm
Posts: 24

PostPosted: Wed Feb 01, 2006 10:13 pm    Post subject: Select the last two characters from a line of output Reply with quote

I was hoping, using a simple pipe and unix command, to be able to cut the last two characters off a line.

example output: 178256
required output: 56

I thought of using the cut command, but can only seem to specify characters from the start of the line. The line is not always the same number of characters.

echo 178256 | cut -c5-
will give 56
as will
echo 178256 | cut -c5-6

but will be incorrect if the say the number was 1785656.

Any suggestions welcome. I am sure there must be a way, there usually is, without having to write a script.
Back to top
View user's profile Send private message
Nigel
LXF regular


Joined: Fri Apr 08, 2005 9:03 pm
Posts: 1141
Location: Gloucestershire, UK

PostPosted: Wed Feb 01, 2006 11:18 pm    Post subject: RE: Select the last two characters from a line of output Reply with quote

Try this...

Code:
echo 178256 | tail -c -3
echo 1785656 | tail -c -3


My experiments suggest that -3 is needed to get the last 2 digits rather than -2; at first I thought it was due to the space before the pipe, but it needs it even without the space there. Perhaps it's the End-Of-Data marker ?
_________________
Hope this helps,

Nigel.
Back to top
View user's profile Send private message
pc



Joined: Fri Apr 08, 2005 6:12 pm
Posts: 24

PostPosted: Thu Feb 02, 2006 7:47 pm    Post subject: Reply with quote

Thanks very much it did the trick. Tried it on both on Redhat and Solaris.

As a point of interest, looking at the tail help, I didn't see why you used the "-" in the "-3". Tried it without, ie:

echo 178256 | tail -c 3
echo 1785656 | tail -c 3

and it worked exactly the same.

Thank-you for you help.
Back to top
View user's profile Send private message
Nigel
LXF regular


Joined: Fri Apr 08, 2005 9:03 pm
Posts: 1141
Location: Gloucestershire, UK

PostPosted: Thu Feb 02, 2006 8:47 pm    Post subject: Reply with quote

According to the man page for tail, the - before the number is optional ; either will count from the end of the input. If you use + instead it will count from the beginning of the input. I tend to put optional things like that in when I'm writing scripts just to make it really obvious what I mean when I come back to it months or years later Wink

Oh, and it works on OS X as well, in case you care...
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast