#!/bin/perl -w use strict; my ( @results, %fields ); my $TABLE= 'Photo'; while(){ s/\0//g; if ( s/(? $table, # naming things is good! fields => $fields, values => $values }; } } my @final = sort{ $a->{fields} cmp $b->{fields} } @results; # the sort is easier to understand open OUT, ">results.txt" or die $!; foreach my $insert ( @final ) { my $fieldNames = $insert->{fields}; if ( ! exists $fields{ $fieldNames } ) { $fields{ $fieldNames } = 1; print @$insert{qw(table fields values)}; # and now the hash slice } } close OUT; __DATA__ go INSERT documents ( did, DocID, Template ) VALUES ( 3093, '000000000000000000003093', 'Photo' ) go SELECT * FROM documents WHERE did=3093 go INSERT INTO Photo (AccessLevelID, ANRNumber, ColorID, ContactSheetID) VALUES (1, '225058', 1, 4) go exec new_int_id 'Photo', 'PhotoID' go exec new_str_id 'documents', 'DocId' go INSERT INTO Photo (QualityID, Remark, UniqueID, VolNum) VALUES (1, 'St. Nicolaasfeest -v. Moorsel', '211357', '1') go