Fetch block of text in huge file


Results 1 to 4 of 4

Thread: Fetch block of text in huge file

  1. #1
    Join Date
    Oct 2000
    Location
    Sweden
    Posts
    494

    Fetch block of text in huge file

    After a server crash all I have now is a large mysqldump-file containing some 500 databases.
    I'm looking for a way to import just a few databases, one by one, on a new server. mysql itself can't do this, it's "all or nothing".
    So I'm trying to grep / sed / awk / whatever a block from mysqldump.sql to a new file. Having tried a lot with no success, I now turn to you, the community
    (I know I can import everything to mysql on some other server, then pull out the databases needed and use those dumps on production server, but A) it's not efficient and B) this thing happens now and then so it would be nice to find the perfect solution.)

    So, in a file I want to catch everything between
    CREATE DATABASE /*!32312 IF NOT EXISTS*/ $1
    and the next
    CREATE DATABASE

    Any ideas?
    In pingvino veritas!

  2. #2
    Join Date
    Oct 2000
    Location
    Sweden
    Posts
    494
    ... after having spent so many hours, and finally asking for help...
    I read my own question, and finally realized the real problem - which was not catching a block of text but interpreting those " /*! " correctly.
    So I simply search for everything between
    <database_name>
    and
    "CREATE DATABASE"

    Problem solved, thanks for listening!

    Oh, and just in case someone is interrested, complete command:
    sed -n /$db_name/,/CREATE\ DATABASE/p mysql_030812.sql > db_name.sql
    In pingvino veritas!

  3. #3
    Join Date
    Jun 2004
    Location
    Newcastle upon Tyne
    Posts
    2,978
    Nice one X !

    Linux is for those who can think and reason it out.
    Linux user started Jun 2004 - No. 361921
    Using a Linux live CD to clone XP
    To install Linux and keep Windows MBR untouched
    Adding extra Linux & Doing it in a lazy way
    A Grub menu booting 100+ systems & A "Howto" to install and boot 145 systems
    Just cloning tips Just booting tips A collection of booting tips

    Judge asked Linux "You are being charged murdering Windoze by stabbing its heart with a weapon, what was it?" Replied Linux "A Live CD"

  4. #4
    Join Date
    Oct 2000
    Location
    Sweden
    Posts
    494
    Thanks saikee!

    Often when I'm stuck with a problem I find that formulating a question actually gives me the answer or at least point me in the right direction.
    This is because then I have to think things through clearly, describing problem and actions exactly and in detail often reveals "mind mistakes".

    As for my solution, I have to add it wasn't quite correct. Since I have given a false solution I'm now adding a correct one.
    The database name exists in other places too, so I actually needed to find how to search for pattern "IF NOT EXISTS*/ `db_name`"
    The '`' surrounding the databasename was the worst problem. Putting the whole sed command in quotes did the trick - escaping it did not help:
    sed -n "/EXISTS\*\/\ \`db_name/,/CREATE\ DATABASE/p" mysql_090812.sql > db_name_090812.sql
    In pingvino veritas!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •