How do I delete a last character from input line under UNIX / Linux using sed tool? My sample scientific input data line is as follows and I'd like to get rid of last + character only:
379100.46+521870.11+363838.75+382869.59+
How do I used sed to delete last + character?
Use it as follows:
echo "379100.46+521870.11+363838.75+382869.59+" | sed '$s/.$//'
If you would like to calculate final result, enter:
echo "379100.46+521870.11+363838.75+382869.59+" | sed '$s/.$//' | bc -l
Featured Articles:
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- My 10 UNIX Command Line Mistakes
- Linux: 20 Iptables Examples For New SysAdmins

- 25 PHP Security Best Practices For Sys Admins
- The Novice Guide To Buying A Linux Laptop
- 10 Greatest Open Source Software Of 2009
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- Top 20 OpenSSH Server Best Security Practices
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Linux Video Editor Software
Facebook it - Tweet it - Print it -


{ 5 comments… read them below or add one }
Hi,
Please help me for this.
input : 379100.46+521870.11+363838.75+382869.59
output : 379100.46+521870.11
I would like remove last the character based last two delimiter. Please help me.
Try it:
echo $text | sed -e ‘s/\(.*\)../\1/’
That was exactly what I was searching for, thanks!
Can you explain why it works, though? I use sed quite often but I don’t know what the
does, I always do
.
Konrad,
The “$” (dollar sign; I usually call it “cash”) is a marker or flag that tells sed to jump to the end of a line and edit something in the vicinity of the last character in it. I believe its counterpart for the beginning of a line is “^” (up-pointing carat), but don’t quote me on that. These markers are part of the sed program itself, which can present a problem only when the characters they represent are the ones you want to “sed out” of a string — sometimes even escaping them doesn’t work in those instances.
Hope this helps.
BZT
mandre on ivorde.ro offers other approaches here.
http://www.ivorde.ro/How_to_remove_first_last_character_from_a_string_using_SED-75.html
Evidently the ‘$s’ syntax at the beginning of the sed command isn’t common or even necessary. A simple
usually suffices.
(The above is from one of the comments, not ivorde’s primary text.)
BZT