We have moved to www.dataGenX.net, Keep Learning with us.

Tuesday, January 14, 2014

Removing last or more character of string


Want to remove last character from a string in Linux, We can do this with help of SED commands.

Use this

a) Remove last char
sed 's/.$//' input
or
sed 's/.\{1\}$//' input

i.e. -
echo 123456 | sed 's/.$//'
12345

echo 123456 | sed  's/.\{1\}$//'
12345


b) Remove last 2 char
sed 's/..$//' input
or
sed 's/.\{2\}$//' input

i.e. -
echo 123456 | sed 's/..$//'
1234

echo 123456 | sed  's/.\{2\}$//'
1234


c) Remove more char from last
sed 's/.\{n\}$//' input
where n = no of char



No comments :

Post a Comment