Sed Delete Last Character

in Categories , , , , , , , last updated July 14, 2009

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:


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

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

Share this on (or read 11 comments/add one below):

11 comment

  1. 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.

  2. 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

    sed "s|text|replacement|g"


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


  4. I have file…called file1 in the below format


    means at the end of some of the lines ,i don’t have comma(,)
    now can i place comma in those lines so that my o/p looks like below


  5. how to add a new column(first column) with variable string value to an existing file

    input file name ….filename1


    o/p file format

    ‘abc’ value will change based on the input to the INPUT file

    can you pls help

  6. 379100.46+521870.11+363838.75+382869.59+

    Deleting the + at the end is ok but what if there’s no + sign there, wouldn’t you delete the 9 then? and how to solve it so that only the + is deleted?

  7. @randomhello:

    No problem, just replace the dot character ‘.’ which stands for “any character” by a ‘+’:

    sed ‘s/+$//’

    Have a question? Post it on our forum!