Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

5
  • 23
    Note that in the general case, it doesn't delete the last character of the string, but the last character of every line of the string. Commented Feb 1, 2016 at 10:23
  • 2
    sed -z 's/.$// will do what you're looking for, working with multiple lines too. Commented Feb 21, 2021 at 11:56
  • 1
    @TWiStErRob I get sed: illegal option -- z on macOS Commented Jun 14, 2021 at 11:30
  • @TWiStErRob: that may remove the last newline... Commented Apr 13, 2022 at 22:45
  • Yes, \n is a character, @LuisA.Florit :) You can amend the regex easily if you know more about your input, for example /.\n$/\n/ in your case to keep the last newline and remove the character before the last newline. I think even /.\n?$/\n/ might be an option to ensure a new line at the end of stream. Commented Apr 14, 2022 at 7:36