Created
October 31, 2012 15:56
-
-
Save garex/3987864 to your computer and use it in GitHub Desktop.
Mysql dump sort keys contraints
This file contains 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/gawk -f | |
#FUNCTIONS | |
function printRecords(array) { | |
if( array[0] != "" ) { | |
asort(array); | |
for( j = length(array); j >= 1 ; j-- ) { | |
lastChar = ""; | |
if (j > 1) { | |
lastChar = ","; | |
} | |
sub(/,$/, "", array[j]); | |
print array[j], lastChar; | |
} | |
delete array; | |
} | |
} | |
#MAIN | |
BEGIN { | |
keyRegex = " KEY.*"; | |
constraintRegex = " CONSTRAINT.*"; | |
aSortedIndex = 0; | |
aSorted[0] = ""; | |
} | |
{ | |
if( match($0, keyRegex) ) { | |
aSorted[aSortedIndex++] = $0; | |
} else if( match($0, constraintRegex) ) { | |
aSorted[aSortedIndex++] = $0; | |
} else { | |
printRecords(aSorted); | |
aSortedIndex = 0; | |
print $0; | |
} | |
} | |
END { | |
} |
Thanks for the heads up I have pushed a new version to github https://raw.github.com/nestoru/pob-recipes/master/common/mysql/mysqldump_sort.gawk which correctly keeps the commas and just removes it from the very last line.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also it would be great if the comma was not on an entirely new line