Extract the last character of a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract the last character of a string
# 1  
Old 07-04-2006
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 ! )
# 2  
Old 07-04-2006
[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!
# 3  
Old 07-04-2006
$ echo "some string" | sed -e "s/^.*\(.\)$/\1/"
g
$
This User Gave Thanks to Hitori For This Post:
# 4  
Old 07-05-2006
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]$

# 5  
Old 07-04-2008
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.
# 6  
Old 07-04-2008
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
# 7  
Old 07-04-2008
Code:
#/bin/ksh93

hugo="abcd"
$ print ${hugo}
abcd
$ print ${hugo: -1}
d
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed searches a character string for a specified delimiter character, and returns a leading or traili

Hi, Anyone can help using SED searches a character string for a specified delimiter character, and returns a leading or trailing space/blank. Text file : "1"|"ExternalClassDEA519CF5"|"Art1" "2"|"ExternalClass563EA516C"|"Art3" "3"|"ExternalClass305ED16B8"|"Art9" ... ... ... (2 Replies)
Discussion started by: fspalero
2 Replies

2. Shell Programming and Scripting

Extract all first numeric character from a string

Hello, I have a file of strings a below:- 4358RYFHD9845 28/COC/UYF984 9834URD 98HJDU I need to extract all the first numeric character of every sting as follows:- 4358 28 9834 thanks to suggest ASAP Regards, Jasi Use code tags, thanks. (7 Replies)
Discussion started by: jassi10781
7 Replies

3. Shell Programming and Scripting

Search String and extract few lines under the searched string

Need Assistance in shell programming... I have a huge file which has multiple stations and i wanted to search particular station and extract few lines from it and the rest is not needed Bold letters are the stations . The whole file has multiple stations . Below example i wanted to search... (4 Replies)
Discussion started by: ajayram_arya
4 Replies

4. Shell Programming and Scripting

How to extract the certain position's character in a string

Suppose there are two files: A, format is like: line1 12 line2 33 line3 6 ... B, format is like: >header taaccctaaccctaaccctaacccaaccccaccccaaccccaaccccaac ccaaccctaaccctaaccctaacccaaccctaaccctaaccctaacccaa ccctcaccctcaccctcaccctcaccctcaccctcaccctcaccctaacc... (1 Reply)
Discussion started by: bioinflix
1 Replies

5. Shell Programming and Scripting

to extract string from main string and string comparison

continuing from my previous post, whose link is given below as a reference https://www.unix.com/shell-programming-scripting/171076-shell-scripting.html#post302573569 consider there is create table commands in a file for eg: CREATE TABLE `Blahblahblah` ( `id` int(11) NOT NULL... (2 Replies)
Discussion started by: vivek d r
2 Replies

6. Shell Programming and Scripting

Extract character from string

ps -eaf | grep “oracleTRLV (LOCAL=NO)” | while read ora_proc do echo $ora_proc done I would like to modify the above shell so that if character 13 and 14 equal "12" to do something. Sorry I'm new to shell:( (14 Replies)
Discussion started by: NicoMan
14 Replies

7. Shell Programming and Scripting

Korn: How to loop through a string character by character

If I have a string defined as: MyString=abcde echo $MyString How can I loop through it character by character? I haven't been able to find a way to index the string so that I loop through it. shew01 (10 Replies)
Discussion started by: shew01
10 Replies

8. Shell Programming and Scripting

extract character + 1

Hi, I would like extract from a file a character or pattern after ( n + 1) a specific pattern (n) . ( i supposed with awk) how could i do ? Thanks in advance. (1 Reply)
Discussion started by: francis_tom
1 Replies

9. UNIX for Advanced & Expert Users

Extract a character

HI Guys, Have a Doubt...... I have a pattern "abcdef" and i need to extract the third character..ie(c) How to achieve it? (10 Replies)
Discussion started by: aajan
10 Replies

10. Shell Programming and Scripting

Need help to extract a string delimited by any special character

I have a string as follows IS*blahblah TED~blahblah etc. I want to list down only IS and TED Can someone help me? (24 Replies)
Discussion started by: kumariak
24 Replies
Login or Register to Ask a Question