Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 10 down vote accepted

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.
share|improve this answer
    
Legend! Thanks :) – Craig Watson Aug 21 '13 at 16:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.