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

You must be logged in to post a comment.

purchase accutane online buy accutane cialis prescription order cialis without prescription buy cheap soma online generic synthroid accutane pills cheapest generic viagra lowest price acomplia propecia without a prescription cheap cialis from usa tablet viagra certified viagra buy cialis without prescription levitra discount cheap viagra in usa lowest price clomid order cheap viagra acomplia sale cialis rx order lasix viagra bangkok cheapest cialis prices online viagra cialis vendors buy soma online clomid sale buy cheap lasix online viagra free sample cialis in us viagra canada buy cialis from us soma no prescription cialis pill buy soma buy viagra in us soma for sale where to order viagra viagra buy drug viagra buy cialis in uk synthroid cheap cheap generic cialis buy cheap synthroid online discount propecia acomplia pharmacy order lasix online buy cialis in us soma without a prescription cheap propecia tablets cheap viagra tablets find cheap cialis cialis sales clomid online stores clomid prices compare viagra prices online cheap cialis no rx lasix without prescription cialis pills cialis purchase online pharmacy viagra buy cheap accutane find viagra on internet cialis order buy viagra cheap cheapest levitra cost of viagra cheap lasix tablets order cialis on internet order viagra in canada zithromax no prescription lowest price zithromax cheapest viagra prices