FreeBSD - Tutorials, Security
Home   Archives   Sitemap   About   Contact

How to remove first/last character from a string using SED

Home NEW! Unix Forum News 100 Tips and Tricks Website Development Server Operating Systems Databases
 Ivorde.ROarrow Server Operating Systems arrowAdministration GuidesarrowHow to remove first/last character from a string using SED 

Article Sections

    Hello, Guest !
User name:
Password:
 
Google

 SATELLITE INTERNET
 FreeBSD Tutorials
 Linux LVM Commands
 Free Shell Accounts
 FreeBSD Project
 FreeBSD Handbook
 Advanced Bash-Scripting Guide
 The OpenBSD Project
 Distrowatch
 FreeBSD Handbook


Apache Webserver Home Page

Blog, intrebari si raspunsuri despre Leasing

Posted on: 18 Mar 2008
Author: mandrei
Section: Server Operating Systems | Administration Guides
Views: 3090
Comments: 17 (Add)

How to remove first/last character from a string using SED
How to remove first/last character from a string using SED, Removing the last three characters from every filename, Removing first character from each filename/string, Removing last character from each filename/string

 



How to remove first/last character from a string using SED

In the context of having a list of files with .exe, .pdf, .jpg, .gif extensions and we need to remove these extensions, we need to use sed:
$ cat files
apache-1.3.39.exe
autoconf-wrapper-20070404.exe
glib-2.12.13.exe
libiconv-1.9.2_2.exe
mysql-client-5.0.45.exe
mysql-server-5.0.45_1.exe
p5-Storable-2.16.exe
pecl-APC-3.0.14.exe
php4-4.4.7_2.exe
php4-ctype-4.4.7_2.exe
php4-exif-4.4.7_2.exe
php4-extensions-1.0.exe
php4-ftp-4.4.7_2.exe
php4-gd-4.4.7_2.exe
php4-mysql-4.4.7_2.exe
php4-overload-4.4.7_2.exe
php4-pcre-4.4.7_2.exe

Removing the last three characters from every filename

$ cat files | sed 's/\(.*\).../\1/'
apache-1.3.39.
autoconf-wrapper-20070404.
glib-2.12.13.
libiconv-1.9.2_2.
mysql-client-5.0.45.
mysql-server-5.0.45_1.
p5-Storable-2.16.
pecl-APC-3.0.14.
php4-4.4.7_2.
php4-ctype-4.4.7_2.
php4-exif-4.4.7_2.
php4-extensions-1.0.
php4-ftp-4.4.7_2.
php4-gd-4.4.7_2.
php4-mysql-4.4.7_2.
php4-overload-4.4.7_2.
php4-pcre-4.4.7_2.

(I used this example in order to make it more obvious).

Removing first character from each filename/string

$ cat files |sed 's/.\(.*\)/\1/'
pache-1.3.39.exe
utoconf-wrapper-20070404.exe
lib-2.12.13.exe
ibiconv-1.9.2_2.exe
ysql-client-5.0.45.exe
ysql-server-5.0.45_1.exe
5-Storable-2.16.exe
ecl-APC-3.0.14.exe
hp4-4.4.7_2.exe
hp4-ctype-4.4.7_2.exe
hp4-exif-4.4.7_2.exe
hp4-extensions-1.0.exe
hp4-ftp-4.4.7_2.exe
hp4-gd-4.4.7_2.exe
hp4-mysql-4.4.7_2.exe
hp4-overload-4.4.7_2.exe
hp4-pcre-4.4.7_2.exe

Using sed and regular expressions, we can replace characters in strings. In the last example I replaced any string in the form |one character followed by any other characters zero or more times| with the |any other characters 0 ore more times| .\(.*\) in regular expressions terms matches any string that starts with any single character, followed by any other character 0-1-2-3... times (the brakets are being escaped by backslay ). Sed replaces this string with a new string that contains everything else after the first character in the previous string.

Removing last character from each filename/string

$ cat files |sed 's/\(.*\)./\1/'
apache-1.3.39.ex
autoconf-wrapper-20070404.ex
glib-2.12.13.ex
libiconv-1.9.2_2.ex
mysql-client-5.0.45.ex
mysql-server-5.0.45_1.ex
p5-Storable-2.16.ex
pecl-APC-3.0.14.ex
php4-4.4.7_2.ex
php4-ctype-4.4.7_2.ex
php4-exif-4.4.7_2.ex
php4-extensions-1.0.ex
php4-ftp-4.4.7_2.ex
php4-gd-4.4.7_2.ex
php4-mysql-4.4.7_2.ex
php4-overload-4.4.7_2.ex
php4-pcre-4.4.7_2.ex

Notice that in this last example the dot |one character| moved after |any other characters zero or more times|, resulting in the last character in the string (filename in this case).

This tutorial is not perfect but I hope it helps.

Bookmarks: Echo "How to remove first/last character from a string using SED" around:
del.icio.usdiggFurlYahooMyWebGoogleBookmarksFaceBookTechnocratti
-------------------advertising-----------------

Other articles in Server Operating Systems / Administration Guides
» How to cut out first last n characters from each file name, from a filelist
» Substitute strings in files with perl, sed or vi(m)
» How to cat a file removing commented lines in Unix/Linux
» Shell scripting - conditions for IF conditional function
» How to rename files/directories to uppercase/lowercase character names




Contact webmaster regarding this article
Register or Login to post your article
Hello, Guest ! You can Login or Register to www.ivorde.ro!

 Post comment:

Name:
Title:
Comment:
Please type the word you see in the image (anti-spam verification). Refresh the page if you don't understand the word.
Verification code
Allowed HTML Tags for comments:<p><strong><em><u><h1><h2><h3><h4><h5><h6><img><li>
<ol><ul><span><div><br><ins><del>

17 comment(s) to How to remove first/last character from a string using SED:

1. Re: How to remove first/last character from a string using SED
remove last point by diurnambule at November 24th, 2011 - 10:49
and to remove last point (test.zip => test and not test.):
sed 's/\(.*\)\..../\1/'

2. Re: How to remove first/last character from a string using SED
How to remove everything except the first characte by Nigel Smith at October 12th, 2011 - 21:17
sed 's/\(.\).*/\1/'

3. Re: How to remove first/last character from a string using SED
Speed Optimised Regexp by Nathan Watson-Haigh at August 05th, 2011 - 01:16
The sed part of your examples can be speed up by optimising the regexp as follows:
Removing the last three characters from line
cat files | sed 's/...$//'
Removing first character from each line
cat files | sed 's/^.//'
Removing last character from each line
cat files | sed 's/.$//'

4. Re: How to remove first/last character from a string using SED
how to display only last character from file by mahesh chadare at July 14th, 2011 - 15:26
how to display only last character from file

5. Re: How to remove first/last character from a string using SED
Unix Admin by Imran Zamir at April 20th, 2011 - 09:20
Very nice tutorial in fact, thx

6. Re: How to remove first/last character from a string using SED
Oracle Database Administrator by malik shahid at February 10th, 2010 - 14:06
It is very helpful. but can you please explain the regular expression working logic

7. Re: How to remove first/last character from a string using SED
gopher by JohnC at February 08th, 2010 - 10:44
Thanks very much .. saved a lot of time, not everybody is an expert in awk,sed etc!

8. Re: How to remove first/last character from a string using SED
Remove the character before the last character wi by Andrei at November 04th, 2009 - 15:50
Remove the character before the last character with SED http://forum.ivorde.ro/remove-the-character-before-the-last-character-with-sed-shell-scripting-t40.html

9. Re: How to remove first/last character from a string using SED
qus.. by srutho at November 04th, 2009 - 11:59
hw to delete the character before the last character in each line of a file????

10. Re: How to remove first/last character from a string using SED
sed by swap at September 14th, 2009 - 11:03
how to remove last but one character in a line using sed

11. Re: How to remove first/last character from a string using SED
Mr. by Tinku at September 13th, 2009 - 16:02
Question1:-How to remove/delete last but one character in everyline in a file using sed command.
question2:-How to remove first character in everyline in a file using sed command.
Questions3:-How to swap 3rd,4th words in a file using sed command

12. Re: How to remove first/last character from a string using SED
nice article by ujjwal b soni at December 24th, 2008 - 10:43
Hi,

Its really a nice article !!!

Cheers,

Ujjwal B Soni

baroda, gujarat, india

13. Re: How to remove first/last character from a string using SED
@sekhar by admin at November 04th, 2008 - 15:12
Check out this article: http://www.ivorde.ro/How_to_cut_out_first_last_n_characters_from_file_name-100.html It shows how to remove the first three lines from each file's name. It can easily be adapted to other tasks, if you need to.

14. Re: How to remove first/last character from a string using SED
how to apply the Command for Directory Files by sekhar at October 17th, 2008 - 12:45
hii dude,
of Course u'r scripts looks correct ,but i dont want to take input from the text files ,Instead of that my directory has so many files , and i need to cut the First three Characters from the Filenames.And the Operation should b Inplace(means the Next time i open the Directory it Should Contains the modified files ...

Thankq

15. Re: How to remove first/last character from a string using SED
cat "folder" ? by Nico at September 18th, 2008 - 20:40
I get a whole lot of "No such file or directory" if I try to do this in a folder.
I'm assuming you were doing this on a text file.
What if you wanted to batch rename a whole bunch of files, taking out the first n characters?

16. Re: How to remove first/last character from a string using SED
To Aerouge by Andrei at June 19th, 2008 - 16:33
You can use the above example also to remove first/last two/three and so on characters from a string. Just use dot(.) characters as more as the number of characters you want to remove or replace. URW

17. Re: How to remove first/last character from a string using SED
Great Tut... Kudos by Aerouge at June 17th, 2008 - 16:48
You have no idea how hard it is to find an article on this topic... and once I found your´s I could stop searching! Great work, percise, good examples and really helpfull!

Thanks a thousand times!

   Latest topics on the forum:
Junos router advertisements - Unix IPv6 stateless autoconfiguration via SLAAC
Juniper SRX firewall debug: packet dropped: for self but not interested
IPv6 enablement for Ivorde.ro
How to force NTP in Debian/Ubuntu to synchronize to IPv4 servers, not IPv6 (default \w both proto)
How to use your Juniper SRX firewall and BGP RTBH to fight some of the spam/bad traffic
FreeBSD - Monitor geom disk i/o statistics using gstat/iostat
Squid clear cache on FreeBSD
Linux: Show UUID of filesystem
IPSEC vpn between IPHONE and Linux/FreeBSD racoon daemon
How to add BIOS boot delay for particular VM in VMWare ESXi 5.x
 
   Most viewed articles:
qmail qmail-scanner/clamav qmail-inject: fatal: qq temporary problem / clamdscan: corrupt or unknown clamd scanner error or memory/resource/perms problem - exit status 512/2 - 17686 views
AIX 5.3 - How to check for failed login attempts - 7005 views
Reloading /etc/profile - how to reload Unix /etc/profile - 6163 views
Set up HTTP PROXY via command line in Linux/FreeBSD - 4788 views
How to change ProFTPd welcome message with ServerIdent - 4414 views

   Latest 10 articles:
FreeBSD - Collect installed hard disk drive information - 19 Mar 2009
Set up FTP PROXY via command line in Linux/FreeBSD - 19 Mar 2009
Set up HTTP PROXY via command line in Linux/FreeBSD - 19 Mar 2009
Qmail relay to smarthost: How to route all mail to a smarthost - 03 Feb 2009
EXIM 4 relay to smarthost: How to route all mail except local domain - 03 Feb 2009
Windows XP: print LISTEN ports and network connections using netstat - 30 Jan 2009
qmail qmail-scanner/clamav qmail-inject: fatal: qq temporary problem / clamdscan: corrupt or unknown clamd scanner error or memory/resource/perms problem - exit status 512/2 - 05 Dec 2008
How to cut out first last n characters from each file name, from a filelist - 04 Nov 2008
Mozilla Firefox3 is now released - 18 Jun 2008
How to switch lower case to upper case and upper case to lower case in a string - 17 Jun 2008


Archives
» 2007  |  June  |  October  |  November  |  December
» 2008  |  January  |  February  |  March  |  April  |  May  |  June  |  November  |  December
» 2009  |  January  |  February  |  March



Home | Archives | Sitemap | About | Contact

Designed and developed by Andrei Manescu. Optimized for Mozilla Firefox.  
Copyright 2007 Andrei Manescu
All trademarks and copyrights on this page are owned by their respective owners. Comments are owned by those who posted them.
Valid W3 Document Valid XHTML 1.0 Transitional Valid CSS! The FreeBSD Project Viewable With Any Browser