0

I would like to write a sed statement that removes the last comma.

DROP TABLE IF EXISTS person;$
CREATE TABLE person ($
  id int(11) NOT NULL,$
  name varchar(500) DEFAULT NULL,$
  gender char(1) DEFAULT NULL,$
  birthdate date DEFAULT NULL,$
  deathdate date DEFAULT NULL,$
  height int(11) DEFAULT NULL,$
) ;$

code snippet is from vim with :set list

The wanted output is

DROP TABLE IF EXISTS person;$
CREATE TABLE person ($
  id int(11) NOT NULL,$
  name varchar(500) DEFAULT NULL,$
  gender char(1) DEFAULT NULL,$
  birthdate date DEFAULT NULL,$
  deathdate date DEFAULT NULL,$
  height int(11) DEFAULT NULL$
) ;$

I tryed to do it like this

sed -e 's@,$)@$)@'

but it does not match

3 Answers3

4

sed process one line of input at a time. You need to append the next line to the current one, perform the substitution and print the pattern space:

sed 'N;s/,\n)/\n)/;P;D' inputfile

For your sample input, this would produce:

DROP TABLE IF EXISTS person;
CREATE TABLE person (
  id int(11) NOT NULL,
  name varchar(500) DEFAULT NULL,
  gender char(1) DEFAULT NULL,
  birthdate date DEFAULT NULL,
  deathdate date DEFAULT NULL,
  height int(11) DEFAULT NULL
) ;
devnull
  • 118,548
  • 33
  • 236
  • 227
0

You can act on files in vim with argdo.

This expression matched last comma: (using /)

/,[\s\n]*)\s*;

Also: VIM, Run a command on multiple files

Community
  • 1
  • 1
Niloct
  • 9,491
  • 3
  • 44
  • 57
0

this may work for you:

awk -v RS="" '{gsub(/,\n\)/,"\n)")}7' file
Kent
  • 189,393
  • 32
  • 233
  • 301