Skip to content

Instantly share code, notes, and snippets.

@wogsland
Last active December 23, 2015 19:59
Show Gist options
  • Save wogsland/6686565 to your computer and use it in GitHub Desktop.
Save wogsland/6686565 to your computer and use it in GitHub Desktop.
This short script takes a file with too much whitespace (broken.txt) and shortens each section of multiple spaces to a single space. Useful if you're sent a hybrid fixed-width/space-delimited file.
#-------------------------------------------------------------------------
# This script fixes an almost fixed width file farked on a number of lines.
# It outputs a nicer space-delimited one.
#-------------------------------------------------------------------------
#!/usr/local/bin/perl
print "Starting...\n";
$rowcount = 0;
open(MRFILE, "broken.txt");
while (<MRFILE>){
#update rowcount
$rowcount++;
print "Now on row ",$rowcount,"\n";
#shorten whitespace
my $myline = $_;
#print "Before: ",$myline;
$myline =~ s/\s+/ /g;
#print "After: ",$myline;
open(MYDATA, ">>fixed.txt");
# write the pipe-delimited line to the new file
print MYDATA $myline,"\n";
# finally, terminate the file
close MYDATA;
}
close LOG;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment