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

FAQ
It is currently Sun Aug 20, 2017 4:47 am


Tips & Tricks, Questions regarding shell scripts, awk, perl, sed and much more.

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

Joined: Tue Aug 04, 2009 9:16 am
Posts: 245

Offline
 

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.





Top
Display posts from previous:  Sort by  
E-mail friendPrint view

Topics related to - "Remove the character before the last character with SED - shell scripting"
 Topics   Author   Replies   Views   Last post 
There are no new unread posts for this topic. How to replace dot or any character with AWK

mandrei99

0

7505

Tue Jan 28, 2014 6:35 am

mandrei99 View the latest post

There are no new unread posts for this topic. Remove empty lines in vi/vim

debuser

0

522

Mon Apr 26, 2010 11:06 am

debuser View the latest post

There are no new unread posts for this topic. SED how to remove multiple white spaces from a string

debuser

0

6900

Thu Feb 25, 2010 7:14 am

debuser View the latest post

There are no new unread posts for this topic. Unix shell - using TR to replace new lines with spaces

debuser

0

4856

Thu Feb 25, 2010 8:08 am

debuser View the latest post

There are no new unread posts for this topic. How to echo newline in Linux/Unix shell

mandrei99

0

16711

Tue Jun 25, 2013 5:58 am

mandrei99 View the latest post

There are no new unread posts for this topic. How to add line numbers in unix shell output

mandrei99

0

841

Fri Oct 31, 2014 5:31 am

mandrei99 View the latest post

 

Who is online
Users browsing this forum: No registered users and 0 guests
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum
Jump to:  
cronNews 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