How to copy a portion of big text files in linux?

Working with big mysql dump file.

I have my sqldump file with this command:

mysqldump –opt -Q -uUSERNAME -p DATABASENAME > /PATH/TO/DUMP.SQL

So it dump all my databases into a single file.

The problem is, when moving to new server I can’t use the same database name (restriction on database naming). So I need to filter the files by line numbers, meaning I want to cut and paste the file in the middle, but that won’t be possible in notepad or windows editor that I know because it will just eat the memory and the PC will just hang doing nothing.

After searching for ways, I got the following lines:

grep -n ‘^Mary’ poem.txt

So it will print out the line numbers where the condition occurs. I change it to:

grep -n ‘^USE’ poem.txt

so it just print out the start of each different databases. What’s next? So this the output:

10: USE `db1`;

3434: USE `db2`;

8996: USE `db3`;

9881: USE `db4`;

So I’m gonna need only db2, I need to know how many lines this database is made up of (8996-3434=5562).

The next command is using this:

grep -A5562 ‘USE `db2`;’ dump.sql >newfile.sql

What it will do is just find the line containing ‘USE `db2`;’ and then pick the next 5562 lines and save it to newfile.sql.

Then I use vi editor to change the database name, then use this line to restore it to new database (even vi editor get a little slow for editing file containing thousands of lines):

mysql -uUSERNAME -p NEWDBNAME < /PATH/TO/NEW/DUMP.SQL

Posted February 4th, 2008 by mightyhitter under Linux

 

One Response to “How to copy a portion of big text files in linux?”

  1. ariffikri Says:

    Best editor I found so far for editing large size text file (like above 50MB) is always VI. Why? coz VI has built-in swap-space management. Try download VIM (an improved version of UNIX’s VI) at http://www.vim.org. FYI, they have Windows version too!!

Leave a Reply