Sed Delete Last Character

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


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 11 comments so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
11 comments… add one
  • Mani Oct 18, 2010 @ 11:47

    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.

  • jupiter2005ster Mar 18, 2011 @ 14:17

    Try it:

    echo $text | sed -e ‘s/\(.*\)../\1/’

  • Konrad Höffner Jul 22, 2011 @ 11:52

    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

    $s/.$

    does, I always do

    sed "s|text|replacement|g"

    .

  • SilversleevesX Nov 14, 2011 @ 4:46

    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

  • SilversleevesX Nov 14, 2011 @ 9:37

    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

    sed ‘s/.$//’

    usually suffices.

    (The above is from one of the comments, not ivorde’s primary text.)

    BZT

  • Manas Mar 12, 2012 @ 16:30

    I have file…called file1 in the below format

    1,abc,2,te1,gem,
    2,3,b,r,4
    4,5,3,t,r,
    9,8,y,6,5,
    7,5,1,e,5

    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

    1,abc,2,te1,gem,
    2,3,b,r,4,
    4,5,3,t,r,
    9,8,y,6,5,
    7,5,1,e,5

    • Manas Mar 12, 2012 @ 16:31

      sorry the o/p should be

      1,abc,2,te1,gem,
      2,3,b,r,4,
      4,5,3,t,r,
      9,8,y,6,5,
      7,5,1,e,5,

  • Manas Mar 12, 2012 @ 16:41

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

    input file name ….filename1
    format

    col1,col2,col3,col4
    1,2,3,4
    5,4,3,2
    m,g,f,r

    o/p file format
    col0,col1,col2,col3,col4
    abc,1,2,3,4
    abc,4,3,2,5
    abc,8,7,6,3
    abc,9,8,6,1

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

    can you pls help

  • randomhello Aug 15, 2012 @ 6:37

    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?

  • Konrad Höffner Aug 15, 2012 @ 12:20

    @randomhello:

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

    sed ‘s/+$//’

  • meena Jan 4, 2016 @ 16:38

    how can i remove last but one character from each line in given file

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum