The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
search from specified charactor space akmix UNIX for Dummies Questions & Answers 6 01-30-2008 07:51 PM
Pick the exact value ahmedwaseem2000 Shell Programming and Scripting 11 01-22-2008 07:06 AM
how to pick distinct records.......... ss4u UNIX for Dummies Questions & Answers 3 01-03-2007 10:39 PM
Please help me pick a machine Gnfanatic SUN Solaris 11 04-11-2006 11:06 AM
insert escape charactor within VIM cin2000 Shell Programming and Scripting 2 03-23-2006 08:46 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-20-2005
Registered User
 

Join Date: Jul 2005
Posts: 30
how to pick out last charactor of a string?

Hi,
Does anyone know how many ways to pick out last charactor of a string?

Thanks!
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 12-20-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,466
What shell?
Reply With Quote
  #3 (permalink)  
Old 12-20-2005
Registered User
 

Join Date: Jul 2005
Posts: 30
pick up last charactor from string

I am using ksh for this program.
But I'd also like to know if there is more than one way to do it.
Even maybe using awk, sed, perl....

Thanks a lot!
Reply With Quote
  #4 (permalink)  
Old 12-20-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,002
Code:
#!/bin/ksh

a='123'
echo "${a#${a%?}}"

# OR
echo "${a}" | sed 's/.*\(.\)/\1/' 

# OR
echo "${a}"  | nawk '{print substr($0, length($0))}'
Reply With Quote
  #5 (permalink)  
Old 12-20-2005
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,243
Or...
Code:
expr "${a}" : '.*\(.\)'
Reply With Quote
  #6 (permalink)  
Old 12-20-2005
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
- or....
Code:
echo "${a}" | tail -2c
BTW - it's -2c as there's a trailing newline too.....

Cheers
ZB
Reply With Quote
  #7 (permalink)  
Old 12-21-2005
Registered User
 

Join Date: Dec 2005
Location: London
Posts: 222
One more..

echo $a | cut -c${#a}
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:18 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0