How to Remove Lines from the Middle of a File Using the Linux Terminal

When you manage your own servers, one of the things you end up needing to do on a semi-regular basis is extract stuff from the middle of a file. Maybe it’s a log file, or you need to extra a single table from the middle of your MySQL backup file, like I did.

To figure out the line numbers, a simple grep -n command did the job (the -n argument outputs the line numbers). This made it easy to figure out what I needed to extract.

grep -n wp_posts howtogeekdb010114.bak | more

Results in something like this, which shows the line numbers over on the left side of the output. Piping everything into “more” makes sure that you can see the first line without it scrolling by. Now you’ve got the line number to start with, and probably the one to end with.

4160:-- Table structure for table `wp_posts`
4163:DROP TABLE IF EXISTS `wp_posts`;
4166:CREATE TABLE `wp_posts` (
4203:-- Dumping data for table `wp_posts`
4206:LOCK TABLES `wp_posts` WRITE;
4207:/*!40000 ALTER TABLE `wp_posts` DISABLE KEYS */;
4208:INSERT INTO `wp_posts` VALUES (1,2,'2006-09-11 05:07:23','2006-09-11

You could, of course, just pipe the output from grep into another file, like this:

grep keyword filename.txt > outputfile

In my case, that didn’t want to work, because I couldn’t import the resulting backup for some reason. So, I found a different way to extract the lines using sed, and this method did work.

sed -n '4160,4209p' howtogeekdb0101140201.bak > outputfile

Basically the syntax is like this, making sure to use the -n argument, and include the “p” after the second line number.

sed -n 'FIRSTLINENUMBER, LASTLINENUMBERp' filename > outputfilename

Some other ways you can pull out specific lines in the middle of a file? You could use the “head” command with the +number argument to just read through the first x lines of a file, and then use tail to extract those lines. Not the best option, lots of overhead. Simpler option? You can use the split command to turn the file into multiple files right at the line number you want, and then extract the lines using head or tail.

Or you can just use sed.

Lowell Heddings, better known online as the How-To Geek, spends all his free time bringing you fresh geekery on a daily basis. You can follow him on if you'd like.