LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 12-27-2005, 05:42 PM   #1
3saul
Member
 
Registered: Aug 2004
Location: Brisbane, Australia
Distribution: Ubuntu/Fedora
Posts: 86

Rep: Reputation: 15
SED - remove last four characters from string


SED - remove last four characters from string (or using any other utility.

Can anybody help me with this problem?

Thanks
 
Old 12-27-2005, 06:02 PM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
For example:
Code:
echo "hello fredrick" |sed 's/.\{4\}$//'
 
Old 07-28-2014, 01:19 AM   #3
miriam-e
LQ Newbie
 
Registered: Nov 2011
Location: QLD, Australia
Distribution: Puppy Linux
Posts: 15

Rep: Reputation: Disabled
One of the great things about Unix/Linux is that there is usually more than one way to do something so you get to choose the one that best suits your purposes.

bash
You can crop characters from the end of a string without having to load another command if your shell is a reasonably modern version of bash:
Code:
string="elephant"
echo "${string:0:${#string}-4}"
That's probably a bit hard to read. Here is the same thing broken into its two parts:
Code:
string="elephant"
len=${#string}-4
echo "${string:0:$len}"
Here is another, simpler way to do it in bash:
Code:
string="elephant"
echo "${string%????}"
head
Probably the simplest and easiest way to do it is using head.
NOTE: I don't think this works with the cut-down version of head in busybox.
Code:
string="elephant"
echo -n "$string" | head -c-4
awk
You can do it with awk, which is a bit ridiculous, though nicely general purpose and very easy to read:
Code:
string="elephant"
echo -n "$string" | gawk '{print substr($0,1,length()-4)}'
Notice that I used the -n option for echo which prevents it appending a newline character, otherwise it would look like only 3 characters were chopped.

sed
I'm a bit of a fan of sed though.
This is fastest:
Code:
string="elephant"
echo "$string" | sed 's/....$//'
This is slower but less tedious if you have lots of characters to remove:
Code:
string="elephant"
echo "$string" | sed 's/.\{4\}$//'
cut
I love this even though it is nuts:
Code:
string="elephant"
echo "$string" | rev | cut -c 5- | rev
If you want to use cut without reversing the string you have to find out the length of the string first then subtract the number of characters you want cropped. This is a hassle, but can still be done fairly easily:
Code:
string="elephant"
let len=${#string}-4
echo "$string" | cut -c 1-$len
or harder to read, but more concisely:
Code:
string="elephant"
echo "$string" | cut -c 1-$((${#string}-4))

Note that for all the examples using an external command you don't really need echo. You can use 'here' redirection like this:
Code:
head -c-5 <<<"$string"

Last edited by miriam-e; 07-28-2014 at 03:57 AM. Reason: added code tags & fixed head example & added more examples
 
Old 07-28-2014, 01:41 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,355

Rep: Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042
Replying after 8 and a half years ?.
I wish jeremy would agree to lock old threads ...
 
Old 07-28-2014, 04:54 AM   #5
miriam-e
LQ Newbie
 
Registered: Nov 2011
Location: QLD, Australia
Distribution: Puppy Linux
Posts: 15

Rep: Reputation: Disabled
It's not so much that I was replying as adding to the collective knowledge in LinuxQuestions.
I think more people use this as a storehouse of information than a conversation. If they want conversations they always have [ugh] facebook.
 
Old 07-28-2014, 06:25 AM   #6
3saul
Member
 
Registered: Aug 2004
Location: Brisbane, Australia
Distribution: Ubuntu/Fedora
Posts: 86

Original Poster
Rep: Reputation: 15
Yes it is over 8.5 years...but others will benefit from the thorough response.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I replace this string with another using sed? dave4545 Programming 7 01-27-2006 10:58 AM
Getting last characters of a line with sed command LULUSNATCH Programming 4 12-21-2005 09:33 AM
insert string with sed greg108 Programming 7 02-18-2005 01:11 PM
using sed to grab two characters? nausicaa3000 Programming 4 04-20-2004 11:01 AM
[sed] replace string? chuanyung Programming 3 03-11-2004 08:42 PM


All times are GMT -5. The time now is 08:51 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration