![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 ! )
|
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
[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
|
||||
|
||||
|
$ echo "some string" | sed -e "s/^.*\(.\)$/\1/"
g $ |
|
#4
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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"
Code:
print - "${hugo##${hugo%%?}}" # will yield "d"
bakunin |
|
#7
|
|||
|
|||
|
Code:
#/bin/ksh93
hugo="abcd"
$ print ${hugo}
abcd
$ print ${hugo: -1}
d
$
|
|||
| Google The UNIX and Linux Forums |