Last active
March 13, 2024 13:17
-
-
Save artifactsauce/8035314 to your computer and use it in GitHub Desktop.
Convert from a pg_dump data file to TSV files for LOAD DATA INFILE.
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 perl | |
use strict; | |
use warnings; | |
use Path::Class; | |
use FindBin; | |
my $output_dir = dir($FindBin::Bin,"data"); | |
$output_dir->is_dir or die "[ERROR] $output_dir is not a directory."; | |
my $input_file = file( $ARGV[0] ); | |
my $ifh = $input_file->open('r') or die $!; | |
while ( my $line = $ifh->getline ) { | |
next unless $line =~/^COPY (\S+)/; | |
my $output_file = $output_dir->file( $1.'.txt' ); | |
my $ofh = $output_file->open('w') or die $!; | |
create_data_file( $ifh, $ofh ); | |
} | |
exit; | |
sub create_data_file { | |
my ( $input, $output ) = @_; | |
while ( my $line = $input->getline ) { | |
last if $line =~/^\\\.$/; | |
print $output $line; | |
} | |
$output->close; | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment