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
Korn: How to loop through a string character by character shew01 Shell Programming and Scripting 10 3 Days Ago 04:58 AM
cut from a particular character to the end of the string grajesh_955 Shell Programming and Scripting 2 05-25-2008 03:03 AM
converting character string to hex string axes High Level Programming 5 09-20-2006 10:04 AM
How do I get the nth character from a string? toughman UNIX for Dummies Questions & Answers 4 06-22-2006 09:54 AM
Searching a string for a character turbulence Shell Programming and Scripting 7 01-12-2006 06:56 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-16-2008
DukeNuke2's Avatar
Soulman
 

Join Date: Jul 2006
Location: Germany, Berlin
Posts: 1,487
how to display only last character of a string?

hi all,
if i do an:
"echo $string |" what should be after the pipe to display ONLY the last char of the output?

tia,
DN2
Reply With Quote
Forum Sponsor
  #2  
Old 01-16-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,933
Depends on your shell:

[zsh]

Code:
zsh-4.3.4% print ${${:-string}[-1]}
g

otherwise:

Code:
echo string|{ read; echo "${REPLY#${REPLY%?}}";}
Reply With Quote
  #3  
Old 01-16-2008
DukeNuke2's Avatar
Soulman
 

Join Date: Jul 2006
Location: Germany, Berlin
Posts: 1,487
thnx! works perfectly fine...
Reply With Quote
  #4  
Old 01-16-2008
Moderator
 

Join Date: Dec 2003
Location: /dev/fl
Posts: 1,061
using sed

Code:
 echo $string | sed -e 's/\(^.*\)\(.$\)/\2/'
Reply With Quote
  #5  
Old 01-16-2008
DukeNuke2's Avatar
Soulman
 

Join Date: Jul 2006
Location: Germany, Berlin
Posts: 1,487
thnx also...
Reply With Quote
  #6  
Old 01-16-2008
Moderator
 

Join Date: Feb 2007
Posts: 2,329
Quote:
Originally Posted by fpmurphy View Post
using sed

Code:
 echo $string | sed -e 's/\(^.*\)\(.$\)/\2/'
1 saved substring suffice:

Code:
sed '/.*\(.$\)/\1/'
With awk:

Code:
awk '{print substr($0,length,1)}'
Regards
Reply With Quote
  #7  
Old 01-16-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,933
GNU Awk:

Code:
$ echo string|awk '$0=$NF' FS=
g

Last edited by radoulov; 01-16-2008 at 04:49 AM.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:44 AM.


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 Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0