Find the answer to your Linux question:
Results 1 to 3 of 3
I need some help with extracting some data from one file and put in another file. Basically I have an sql file that has bunch of tables and data. and ...
  1. #1
    Just Joined!
    Join Date
    Feb 2008
    Posts
    1

    how to extract data from a file

    I need some help with extracting some data from one file and put in another file.
    Basically I have an sql file that has bunch of tables and data. and looks something like this:

    CREATE TABLE `table1` ...
    (table structure here)
    (inserts data here)

    CREATE TABLE `table1` ...
    (table structure here)
    (inserts data here)

    CREATE TABLE `table3` ...
    (table structure here)
    (inserts data here)


    and goes on for about 100 tables.. Im trying to figure out out to extract a specific table and put into a separate file.
    For example, if i wanted to copy table50 to anther file, what would be the steps to find what line table 50 starts, and what line table 51 starts, as the ending point of table 50, and then put those lines into another file.
    Any help would be appreciated. thanks

  2. #2
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Code:
    awk 'BEGIN{RS=""}/table50/' file > outfile

  3. #3
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    The solution of Ghostdog74 assumes you only have an empty line between each sql-statement.
    If you also have empty lines within a statement you may try this if the first 3 fields of the first line look like:

    CREATE TABLE table1 ...

    Code:
    awk -v s="table1" '
    $1=="CREATE" && !f{if($3==s){f=1;print;next}}
    $1=="CREATE" && f{exit}
    f' file > another_file
    where s is a variable with tabel name.

    Regards

Posting Permissions

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