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:
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.
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).
Hello, Guest ! You can Login or Register to www.ivorde.ro!
Post comment:
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 - 04: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 - 15: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 04th, 2011 - 19: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 - 09: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 - 03: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 - 08: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 - 04: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 - 09: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 - 05: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 - 05: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 - 10: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 - 04: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 - 09: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 - 06: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 - 14: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 - 10: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 - 10: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!
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.