Skip to content

Instantly share code, notes, and snippets.

@sheran
Created October 23, 2012 09:26
Show Gist options
  • Save sheran/3937855 to your computer and use it in GitHub Desktop.
Save sheran/3937855 to your computer and use it in GitHub Desktop.
Remove a table from a Gzipped MySQL dump file
#!/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()
@sheran
Copy link
Author

sheran commented Oct 23, 2012

Provided the SQL dump file is called out.sql.gz and resides in the current directory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment