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.

Profile Photo for Lowell Heddings Lowell Heddings
Lowell is the founder and CEO of How-To Geek. He’s been running the show since creating the site back in 2006. Over the last decade, Lowell has personally written more than 1000 articles which have been viewed by over 250 million people. Prior to starting How-To Geek, Lowell spent 15 years working in IT doing consulting, cybersecurity, database management, and programming work.
Read Full Bio »