Last active
December 23, 2015 19:59
-
-
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 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
#------------------------------------------------------------------------- | |
# 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