Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove the last character of the last line of a file? [duplicate]

Tags:

file

linux

bash

I have this set of lines in a file:

{info},
{info},
{info},
{info},

and I want the file like this without the last ",":

{info},
{info},
{info},
{info}

How can I do it in bash? Any idea?

like image 348
Joan Triay Avatar asked Nov 12 '15 19:11

Joan Triay


1 Answers

You can use sed:

sed '$ s/.$//' your_file
  • First $ is to tell sed to match only last line
  • s is for "substitute", note the empty string between the two last /s
  • .$ is a regex that matches the last character in the file

Note that you can use whatever separator you want instead of /, for example you can rewrite the expression:

sed '$ s-.$--' your_file
like image 158
Maroun Avatar answered Oct 21 '22 04:10

Maroun