简体   繁体   中英

Using sed to match comma newline endbracket

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

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
) ;

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

这可能为您工作:

awk -v RS="" '{gsub(/,\n\)/,"\n)")}7' file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM