If I have the following text within a file (somefile.txt):CREATE TABLE table_name (   id int(11) unsigned NOT NULL,   some_field varchar(10),);CREATE TABLE another_table (   id int(11) unsigned NOT NULL,   some_other_field varchar(10),);I want to remove the last trailing comma in each statement, so that it becomes:CREATE TABLE table_name (   id int(11) unsigned NOT NULL,   some_field varchar(10));CREATE TABLE another_table (   id int(11) unsigned NOT NULL,   some_other_field varchar(10));I've used the regular expression \,$\n\) but I can't seem to get this to work with sed, which throws:sed: -e expression #1, char 23: Unmatched ) or \)when I use:sed -i -e 's/\,$\n\)/)/g' somefile.txt
Remove last comma within a pattern
sed;awk;regular expression
If the syntax of the file is everywhere likein the examples, you can use sed -i -n -e '1h;1!H;${g;s/\,\n);/\n);/g;p}' somefile.txtExplanation: 1h           # copy first line the hold buffer1!H          # not first line -> append to the hold buffer${           # execute at the end   g          # copy hold buffer back to pattern buffer   s/ ... /   # multiline replacement in pattern buffer   p          # print pattern buffer}(see also http://austinmatzko.com/2008/04/26/sed-multi-line-search-and-replace/)This way, the whole file is read, kept and modified in memory, if the file is too large for this, a different way need to be chosen.