LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 12-27-2005, 06:02 PM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
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: 29

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
 
4 members found this post helpful.
Old 07-28-2014, 01:41 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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: 29

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.
 
1 members found this post helpful.
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.
 
Old 07-05-2015, 12:39 AM   #7
cprior
LQ Newbie
 
Registered: Jul 2015
Posts: 1

Rep: Reputation: Disabled
I just registered to say that this thread was on the first page of a Google search for the searchterms "bash remove trailing two characters".

Researching these kinds of snippets of wisdom is something I do very frequently. And yes, I see these unnecessary "never push an old thread" replies a lot.

Why?

The internet is bigger than a forum, and the Google clicks will pay the bill.
This "never push an old thread" is outdated and should actively be discouraged.
 
Old 07-05-2015, 07:03 AM   #8
miriam-e
LQ Newbie
 
Registered: Nov 2011
Location: QLD, Australia
Distribution: Puppy Linux
Posts: 29

Rep: Reputation: Disabled
Thanks cprior. Woo hoo!
I really appreciate the note.
 
Old 12-14-2015, 12:45 PM   #9
jacquesbezuidenhout
LQ Newbie
 
Registered: Dec 2015
Posts: 2

Rep: Reputation: Disabled
sorting movies into folders

thanks for the detailed methods of removing the last few characters. i found this especially useful to sorting movies. i had a folder full of movies, not in their own folders, but i wanted them to be, here's how i did it:

~/2TB/Movies$ for a in *.mkv; do mkdir "${a%????}"; mv "$a" "${a%????}"; done
or a little easier to read, you can do it like this:

for a in *.mkv; do \
mkdir "${a%????}" \
mv "$a" "${a%????}" \
done

and then i just proceeded to do the same with the other file types, .mp4, .mkv, etc...
for a in *.mp4; do mkdir "${a%????}"; mv "$a" "${a%????}"; done

now that i think about it, I could have done the following, everything in one command. see the *.{mp4,mkv,avi}
for a in *.{mp4,mkv,avi}; do mkdir "${a%????}"; mv "$a" "${a%????}"; done
 
Old 12-14-2015, 04:43 PM   #10
miriam-e
LQ Newbie
 
Registered: Nov 2011
Location: QLD, Australia
Distribution: Puppy Linux
Posts: 29

Rep: Reputation: Disabled
Nice, Jacques. I wouldn't have thought of using
Quote:
for a in *.{mp4,mkv,avi}
Very neat solution. I'm always having to organise folders of converted pictures when I do mass-conversion with ImageMagick. This could save me some time and effort. Thank you.
 
Old 12-04-2018, 12:24 PM   #11
jdh239
LQ Newbie
 
Registered: Nov 2007
Posts: 7

Rep: Reputation: 0
2018 and still useful. Thx!
 
Old 01-14-2023, 06:17 PM   #12
snmpd
LQ Newbie
 
Registered: Jan 2023
Posts: 1

Rep: Reputation: 0
I am pretty happy that Jeremy didn't lock this thread like syg00 wanted. I signed up just to post on this thread and keep it alive.

It's 2023 and and I found the 2014 post from miriam-e more helpful than the 2005 post.
 
Old 01-16-2023, 10:21 AM   #13
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,790

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
At this occasion lemme add that
Code:
echo "${string:0:${#string}-4}"
is shorter
Code:
echo "${string:0: -4}"
There must be a space, otherwise bash would see the :- modifier (that is much different). Another way is
Code:
echo "${string:0:(-4)}"
 
  


Reply



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:39 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration