Multi-Line Sed Replace
Solution 1:
AWK answer
With your sample text in a file named sql
, the following pattern (with line breaks and indentation for clarity):
awk -v skip=1 '{
if (skip) { skip=0 }
else {
if (/FULLTEXT KEY/) { skip=1; sub(/,$/, "", prevline) }
print prevline
}
prevline=$0
}
END { print prevline }' sql
produces:
CREATE TABLE `table` (
`id` int(10) NOT NULL auto_increment,
`name` varchar(100) NOT NULL default '',
`description` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
Explanation:
- We implement "lookahead" by only printing the previously encountered line at every iteration, after inspecting the current line.
- If the current line contains the
FULLTEXT KEY
marker, we set a flag to skip printing this line during the next iteration. We also remove the trailing comma on the previous line that is about to be printed. - We skip printing an empty initial line (before
prevline
has been set) by initially settingskip
to1
("true"). - We make sure to print the last line by ending the script with an extra
prevline
print. Note that the current implementation assumes that this last line is not a line at risk of being skipped, i.e. that it does not contain theFULLTEXT KEY
marker.
Original (incomplete) sed
answer
This answer is incomplete and certainly in most cases incorrect, since sed
will consume the input stream too quickly for the intended result when doing multiline matching -- as pointed out in the comments, it will only work for matches on even numbered rows! sed
does not have "true" lookahead functionality, so we would be better off using Python/Perl/etc., or indeed AWK as above.
With your sample text in a file named sql
, the following pattern:
$ sed 'N; s/,\n FULLTEXT.*//' sql
produces:
CREATE TABLE `table` (
`id` int(10) NOT NULL auto_increment,
`name` varchar(100) NOT NULL default '',
`description` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
Explanation:
-
N
enables multiline matching. -
\n
represents a line break. -
s/pattern/replacement/
is the standard replacement syntax. -
.*
will match anything to the end of the current line.