Consider the following text (incidentally, part of a MySQL dump):
CREATE TABLE `table` ( `id` int(10) NOT NULL auto_increment, `name` varchar(100) NOT NULL default '', `description` text NOT NULL, PRIMARY KEY (`id`), FULLTEXT KEY `full_index` (`name`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */;
I would like to remove the FULLTEXT
key, and I also want to remove the trailing comma on the line above so that the SQL remains valid.
Can anyone come up with (and explain) a sed
recipe to do this?
Best Answer
AWK answer
With your sample text in a file named
sql
, the following pattern (with line breaks and indentation for clarity):produces:
Explanation:
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.prevline
has been set) by initially settingskip
to1
("true").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
answerThis 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:produces:
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.