Linux, FreeBSD, Juniper, Cisco / Network security articles and troubleshooting guides
https://forum.ivorde.com/

Remove the character before the last character with SED - shell scripting
https://forum.ivorde.com/remove-the-character-before-the-last-character-with-sed-shell-scripting-t40.html
Page 1 of 1

Author:  mandrei99 [ Wed Nov 04, 2009 10:49 am ]
Post subject:  Remove the character before the last character with SED - shell scripting

Removing the character before the last character with SED - shell scripting.

Today I received a comment to my "How to remove first/last character from a string using SED" article: http://www.ivorde.ro/How_to_remove_first_last_character_from_a_string_using_SED-75.html

Similar to removing the last character (cat files |sed 's/\(.*\)./\1/'), here's how to remove the character before the last character.

Below is a simple rpm -qa output
Code:
# rpm -qa | head
libgcc-3.2.3-54
setup-2.5.27-1
basesystem-8.0-2
glibc-2.3.2-95.39
bzip2-libs-1.0.2-11.EL3.4
cracklib-2.7-22
e2fsprogs-1.32-15.1
ethtool-1.8-3.3
gdbm-1.8.0-20
glib2-2.2.3-2.0


So we want to remove the "5", "-", "-", "3" and so on characters (before the last characters) of each line:
Code:
# rpm -qa | head  | sed 's/\(.*\)\(.\{1\}\)\(.\{1\}\)/\1\3/g'
libgcc-3.2.3-4
setup-2.5.271
basesystem-8.02
glibc-2.3.2-95.9
bzip2-libs-1.0.2-11.EL34
cracklib-2.7-2
e2fsprogs-1.32-151
ethtool-1.8-33
gdbm-1.8.0-0
glib2-2.2.3-20


Similar code is for removing the second character in a string.

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/