10

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?

Joan Triay
  • 1,232
  • 5
  • 16
  • 31

2 Answers2

24

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
Maroun
  • 87,488
  • 26
  • 172
  • 226
1

In vim, you could use :substitute or :s to do this as well. It's a vim-built in -- you can read about it here http://vim.wikia.com/wiki/Search_and_replace

The syntax is very similar to the other posted solution, you'd go into the command mode and type in :$s/.$//g

Lando
  • 373
  • 2
  • 8