Created
October 23, 2012 09:26
-
-
Save sheran/3937855 to your computer and use it in GitHub Desktop.
Remove a table from a Gzipped MySQL dump file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import fileinput | |
import re | |
import gzip | |
def checkmarker1(line, table_name): | |
line = line.strip('\n') | |
needle = re.compile('.*?Dumping data for table \`'+table_name+'\`.*') | |
return needle.match(line) | |
def checkmarker2(line): | |
line = line.strip('\n') | |
needle = re.compile('.*?UNLOCK TABLES;.*') | |
return needle.match(line) | |
outfile = gzip.open('out.sql.gz','wb') | |
data = fileinput.FileInput(openhook=fileinput.hook_compressed) | |
marker1 = False | |
marker2 = False | |
ctr = 0 | |
for line in data: | |
m1 = checkmarker1(line,'wp_config') | |
if m1 is not None: | |
marker1 = True | |
if marker1 is True: | |
ctr += 1 | |
m2 = checkmarker2(line) | |
if m2 is not None: | |
marker1 = False | |
marker2 = False | |
if marker1 is False and marker2 is False: | |
outfile.write(line) | |
print "Skipped "+str(ctr)+" lines" | |
outfile.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Provided the SQL dump file is called out.sql.gz and resides in the current directory