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: 8514
Comments: 12 (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
DNS query tools - Network tools Ping - Traceroute How to remove first/last character from a string using SEDIn 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>

12 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
Oracle Database Administrator by malik shahid at February 10th, 2010 - 15:06
It is very helpful. but can you please explain the regular expression working logic

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

3. 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 - 16: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

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

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

6. Re: How to remove first/last character from a string using SED
Mr. by Tinku at September 13th, 2009 - 17: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

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

Its really a nice article !!!

Cheers,

Ujjwal B Soni

baroda, gujarat, india

8. Re: How to remove first/last character from a string using SED
@sekhar by admin at November 04th, 2008 - 16: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.

9. 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 - 13: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

10. Re: How to remove first/last character from a string using SED
cat "folder" ? by Nico at September 18th, 2008 - 21: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?

11. Re: How to remove first/last character from a string using SED
To Aerouge by Andrei at June 19th, 2008 - 17: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

12. Re: How to remove first/last character from a string using SED
Great Tut... Kudos by Aerouge at June 17th, 2008 - 17: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:
ffmpeg: error while loading shared libraries: libavdevice.so.52: cannot open shared object file: No
FFMPEG svn 24953 compile fails videodev.h:56: error: expected specifier-qualifier-list before ‘ulong
Apache to Nginx Server parameters translation with php function
Rename MySql table
Mysql - Change position of a column after another column
Mysql change column's default value
Postfix change message size limit
Pool for spam
Mysql remove column from an existing table
Mysql add Unique index to a table column
 
   Most viewed articles:
How to remove first/last character from a string using SED - 8514 views
Reloading /etc/profile - how to reload Unix /etc/profile - 5763 views
How to clear/reset DNS cache on Windows XP / Linux - 5307 views
How to check if an rpm package is installed on a Red Hat Linux server - 4000 views
FreeBSD: Add/remove an additional IP alias - 3838 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