Linux, FreeBSD, Juniper, Cisco / Network security articles and troubleshooting guides

FAQ
It is currently Wed Nov 13, 2019 4:35 pm


Username:
Subject:
Message body:
Enter your message here, it may contain no more than 60000 characters. 

Smilies
:D :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :arrow: :| :mrgreen: :geek: :ugeek:
Font size:
 
Font colour
Options:
BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON
Disable BBCode
Disable smilies
Do not automatically parse URLs
Confirmation code
Confirmation code:
In an effort to prevent automatic submissions, we require that you enter both of the words displayed into the text field underneath.
     

Topic review - Remove the character before the last character with SED - shell scripting
Author Message
Post subject: Remove the character before the last character with SED - shell scripting  |  Post Posted: Wed Nov 04, 2009 10:49 am
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.
Jump to:  
News News Site map Site map SitemapIndex SitemapIndex RSS Feed RSS Feed Channel list Channel list


Delete all board cookies | The team | All times are UTC - 5 hours [ DST ]



phpBB SEO