Extracting a line section of mysql backup using sed

Posted by carpii on Server Fault See other posts from Server Fault or by carpii
Published on 2012-10-07T15:58:52Z Indexed on 2012/10/07 21:40 UTC
Read the original article Hit count: 188

Filed under:
|
|
|

I occasionally need to extract a single record from a mysqlbackup

To do this, I first extract the single table I want from the backup...

sed -n -e '/CREATE TABLE.*usertext/,/CREATE TABLE/p' 20120930_backup.sql > table.sql

In table.sql, the records are batched using extended inserts (with maybe 100 records per insert before it creates a new line starting with INSERT INTO), so they look like...

INSERT INTO usertext VALUES (1, field2 etc), (2, field2 etc),

INSERT INTO usertext VALUES (101, field2 etc), (102, field2 etc), ...

Im trying to extract record 239560 from this, using...

sed -n -e '/(239560.*/,/)/p' table.sql > record.sql

Ie.. start streaming when it finds 239560, and stop when it hits the closing bracket

But this isnt working as I hoped, it just results in the full insert batch being output.

Please can someone give me some pointers as to where Im going wrong?

Would I be better off using awk for extracting segments of lines, and use sed for extracting lines within a file?

© Server Fault or respective owner

Related posts about mysql

Related posts about grep