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 19 Hours Ago 04:58 AM
How to extract first column with a specific character selamba_warrior Shell Programming and Scripting 3 05-22-2008 02:14 AM
extract character + 1 francis_tom Shell Programming and Scripting 1 04-21-2008 09:16 AM
Extract a character aajan UNIX for Advanced & Expert Users 10 08-20-2007 07:03 AM
Need help to extract a string delimited by any special character kumariak Shell Programming and Scripting 24 06-03-2005 06:20 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 07-04-2006
Registered User
 

Join Date: Jul 2006
Posts: 7
Extract the last character of a string

How can I extract the last character of a string (withou knowing how many characters are in that string ! )
Reply With Quote
Forum Sponsor
  #2  
Old 07-04-2006
Registered User
 

Join Date: Jun 2006
Posts: 15
[muru:~]cat lastchar
word=$1
num=`echo $word | wc -c `
let num-=1
last=`echo $1 | cut -c$num`
echo $last

assumptin: the string that comes after scriptname (its lastchar here) is the string for which u need the last charactor for. Hope this is helpful.

It could be done in a simple way also, not sure how for now!
Reply With Quote
  #3  
Old 07-04-2006
Hitori's Avatar
Registered User
 

Join Date: Jun 2006
Posts: 360
$ echo "some string" | sed -e "s/^.*\(.\)$/\1/"
g
$
Reply With Quote
  #4  
Old 07-04-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,698
You can use the ksh builtin typeset feature.

Code:
[/tmp]$ cat try.ksh
#! /bin/ksh

typeset -R1 right

STR="some string"
right=$STR
echo $STR
echo $right

[/tmp]$ ./try.ksh
some string
g
[/tmp]$
Reply With Quote
  #5  
Old 07-04-2008
Registered User
 

Join Date: Jul 2008
Posts: 1
Extract the last character of a string

I guess this is a common question and most people suggest the use of sed or awk. so here's my contribution:
Two commands which typically are forgotten is head and tail which allow also to process standard input if you ommit a file.
For instance, if you want a string without the last character you can do:
echo -n "my string discard" | head -c -1
the result is "my string discar"

if you want the last character of a string use tail instead:
echo -n "my string discard"| tail -c -1
the resut is "d"

notes:
-the use of -n in echo avoids a newline in the output
-in the -c option the value can be positive or negative, depending how you want to cut the string.

Carlos.
Reply With Quote
  #6  
Old 07-04-2008
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,256
The following is assuming Korn Shell and might not work if you shell is very different from the ksh.

To chop off the last character of a string use ${var%%?}. Example:

Code:
hugo="abcd"
print - ${hugo%%?}       # will yield "abc"
To display the last character only (without using any external tools) you can nest the above construction:

Code:
print - "${hugo##${hugo%%?}}"     # will yield "d"
I hope this helps.

bakunin
Reply With Quote
  #7  
Old 07-04-2008
Moderator
 

Join Date: Dec 2003
Location: /dev/fl
Posts: 1,059
Code:
#/bin/ksh93

hugo="abcd"
$ print ${hugo}
abcd
$ print ${hugo: -1}
d
$
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 12:23 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