Operating System - HP-UX
1834196 Members
2588 Online
110065 Solutions
New Discussion

Re: cut last character in a string

 
SOLVED
Go to solution
Coolmar
Esteemed Contributor

cut last character in a string

testc
namec
test2c

How do I take the "c" off the end of each string?
5 REPLIES 5
Matti_Kurkela
Honored Contributor
Solution

Re: cut last character in a string

sed -e 's/.$//g' < with-c.txt > without-c.txt

MK
MK
A. Clay Stephenson
Acclaimed Contributor

Re: cut last character in a string

Here is one approach using cut.

IN=testc
OUT=$(echo "${IN}" | cut -c 1-$((${#IN} - 1)))
echo "IN = \"${IN}\""
echo "OUT = \"${OUT}\""
If it ain't broke, I can fix that.
Ivan Krastev
Honored Contributor

Re: cut last character in a string

cat file | cut -b -1 > file2


regards,
ivan
Coolmar
Esteemed Contributor

Re: cut last character in a string

Thanks
Peter Nikitka
Honored Contributor

Re: cut last character in a string

Hi,

pure shell builtins (I used ksh):

a=stringwithlastg
typeset -R1 ll=$a
echo ${a%*${ll}}
stringwithlast

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"