use strict; use warnings; # Locate header: discard lines until we find # a line containing "-----" while () { last if /-----/; } # Gather the headings my @headings; while () { last if /=====/; my @fields = split /%/; for my $i (0 .. $#fields) { $headings[$i] .= $fields[$i]; } } print join(", ", @headings), "\n"; # Extract the data while () { last if /-----/; my @fields = split /%/; print join(", ", @fields), "\n"; } #### $ perl 1103507.pl *----------*--------* , Trm 4 Trm7 , Trm 5 Trm , Trm9 , TrmY0 , , 0.021 , -0.X , 0.0 , 5.X , , -0.063 , -0.4 , 0.0 , 5.6 , , 0.008 , -0.X , 0.0 , 5.8 , , -0.065 , -0.5 , 0.0 , 5.9 , , 0.009 , -0.X , 0.0 , 6.0 , , -0.066 , -0.4 , 0.0 , 6.Y , , 0.007 , -0.X , 0.0 , 6.Y , , -0.065 , -0.5 , 0.0 , 6.X , , 0.006 , -0.X , 0.0 , 6.X , , -0.065 , -0.5 , 0.0 , 6.3 , , 0.005 , -0.3 , 0.0 , 6.3 , , -0.069 , -0.5 , 0.0 , 6.3 , , 0.003 , -0.X , 0.0 , 6.4 , , -0.068 , -0.4 , 0.0 , 6.4 , , 0.003 , -0.3 , 0.0 , 6.4 , , -0.07Y , -0.5 , 0.0 , 6.4 , , 0.00X , -0.X , 0.0 , 6.4 , , -0.07Y , -0.5 , 0.0 , 6.4 , , 0.00Y , -0.3 , 0.0 , 6.4 , , -0.07Y , -0.4 , 0.0 , 6.5 , , 0.003 , -0.X , 0.0 , 6.5 , , -0.07Y , -0.4 , 0.0 , 6.5 , , 0.00X , -0.X , 0.0 , 6.5 , , -0.07Y , -0.5 , 0.0 , 6.5 , , 0.00Y , -0.3 , 0.0 , 6.5 , #### s/^\s+// for @fields; # remove leading blanks s/\s+$// for @fields; # remove trailing blanks #### $ perl 1103507.pl *----------*--------*, Trm 4Trm7, Trm 5Trm, Trm9, TrmY0, , 0.021, -0.X, 0.0, 5.X, , -0.063, -0.4, 0.0, 5.6, , 0.008, -0.X, 0.0, 5.8, , -0.065, -0.5, 0.0, 5.9, , 0.009, -0.X, 0.0, 6.0, , -0.066, -0.4, 0.0, 6.Y, , 0.007, -0.X, 0.0, 6.Y, , -0.065, -0.5, 0.0, 6.X, , 0.006, -0.X, 0.0, 6.X, , -0.065, -0.5, 0.0, 6.3, , 0.005, -0.3, 0.0, 6.3, , -0.069, -0.5, 0.0, 6.3, , 0.003, -0.X, 0.0, 6.4, , -0.068, -0.4, 0.0, 6.4, , 0.003, -0.3, 0.0, 6.4, , -0.07Y, -0.5, 0.0, 6.4, , 0.00X, -0.X, 0.0, 6.4, , -0.07Y, -0.5, 0.0, 6.4, , 0.00Y, -0.3, 0.0, 6.4, , -0.07Y, -0.4, 0.0, 6.5, , 0.003, -0.X, 0.0, 6.5, , -0.07Y, -0.4, 0.0, 6.5, , 0.00X, -0.X, 0.0, 6.5, , -0.07Y, -0.5, 0.0, 6.5, , 0.00Y, -0.3, 0.0, 6.5, #### my @fields = split /\s*%\s*/; #### next if $fields[$i] =~ /----/; #### $ perl 1103507.pl , Trm 4Trm7, Trm 5Trm, Trm9, TrmY0 , 0.021, -0.X, 0.0, 5.X , -0.063, -0.4, 0.0, 5.6 , 0.008, -0.X, 0.0, 5.8 , -0.065, -0.5, 0.0, 5.9 , 0.009, -0.X, 0.0, 6.0 , -0.066, -0.4, 0.0, 6.Y , 0.007, -0.X, 0.0, 6.Y , -0.065, -0.5, 0.0, 6.X , 0.006, -0.X, 0.0, 6.X , -0.065, -0.5, 0.0, 6.3 , 0.005, -0.3, 0.0, 6.3 , -0.069, -0.5, 0.0, 6.3 , 0.003, -0.X, 0.0, 6.4 , -0.068, -0.4, 0.0, 6.4 , 0.003, -0.3, 0.0, 6.4 , -0.07Y, -0.5, 0.0, 6.4 , 0.00X, -0.X, 0.0, 6.4 , -0.07Y, -0.5, 0.0, 6.4 , 0.00Y, -0.3, 0.0, 6.4 , -0.07Y, -0.4, 0.0, 6.5 , 0.003, -0.X, 0.0, 6.5 , -0.07Y, -0.4, 0.0, 6.5 , 0.00X, -0.X, 0.0, 6.5 , -0.07Y, -0.5, 0.0, 6.5 , 0.00Y, -0.3, 0.0, 6.5 #### print join(", ", map {qq("$_")} @headings), "\n"; #### # Extract the data while () { last if /-----/; my @fields = split /\s*%\s*/; for my $i (0 .. $#fields) { if ($fields[$i] =~ /[^-\+\.0-9]/) { # has non-numeric characters, so quote it $fields[$i] = qq("$fields[$i]"); } } print join(", ", @fields), "\n"; } #### "", "Trm 4Trm7", "Trm 5Trm", "Trm9", "TrmY0" , 0.021, "-0.X", 0.0, "5.X" , -0.063, -0.4, 0.0, 5.6 , 0.008, "-0.X", 0.0, 5.8 , -0.065, -0.5, 0.0, 5.9 , 0.009, "-0.X", 0.0, 6.0 , -0.066, -0.4, 0.0, "6.Y" , 0.007, "-0.X", 0.0, "6.Y" , -0.065, -0.5, 0.0, "6.X" , 0.006, "-0.X", 0.0, "6.X" , -0.065, -0.5, 0.0, 6.3 , 0.005, -0.3, 0.0, 6.3 , -0.069, -0.5, 0.0, 6.3 , 0.003, "-0.X", 0.0, 6.4 , -0.068, -0.4, 0.0, 6.4 , 0.003, -0.3, 0.0, 6.4 , "-0.07Y", -0.5, 0.0, 6.4 , "0.00X", "-0.X", 0.0, 6.4 , "-0.07Y", -0.5, 0.0, 6.4 , "0.00Y", -0.3, 0.0, 6.4 , "-0.07Y", -0.4, 0.0, 6.5 , 0.003, "-0.X", 0.0, 6.5 , "-0.07Y", -0.4, 0.0, 6.5 , "0.00X", "-0.X", 0.0, 6.5 , "-0.07Y", -0.5, 0.0, 6.5 , "0.00Y", -0.3, 0.0, 6.5 #### shift @fields; #### . . . snip . . . shift @headings; print join(", ", map {qq("$_")} @headings), "\n"; # Extract the data while () { last if /-----/; my @fields = split /\s*%\s*/; shift @fields; for my $i (0 .. $#fields) { if ($fields[$i] =~ /[^-\+\.0-9]/) { # has non-numeric characters, so quote it $fields[$i] = qq("$fields[$i]"); } } print join(", ", @fields), "\n"; } . . . snip . . . #### # table_2_csv.pl # # Read a table (formatted similar to the one in the # DATA section) and reformat it into a CSV file. # # TODO: # 1) Make it actually open a file and process it, # instead of only looking at the DATA section. # 2) Maybe pull the file reading and column split # into a separate function to handle the extra # column 0 symptom. # use strict; use warnings; # Locate header: discard lines until we find # a line containing "-----" while () { last if /-----/; } # Gather the headings my @headings; while () { last if /=====/; my @fields = split /\s*%\s*/; for my $i (0 .. $#fields) { next if $fields[$i] =~ /----/; $headings[$i] .= $fields[$i]; } } print_as_csv(@headings); # Extract the data while () { last if /-----/; my @fields = split /\s*%\s*/; print_as_csv(@fields); } sub print_as_csv { my @data = @_; # Band-aid to handle our borders: first column # is always empty, so get rid of it shift @data; # Quote everything that's not a number (it *is* # supposed to be a CSV file!) for my $i (0 .. $#data) { if ($data[$i] =~ /[^-\+\.0-9]/) { # has non-numeric characters, so quote it $data[$i] = qq("$data[$i]"); } } print join(", ", @data), "\n"; } __DATA__ TEXT MATCHING header here! . . . snip: no reason to repeat the data . . . #### *------%------%-------------% % % % Col3 % Col4 % % Col1 % Col2 *------*------* % % % Bar % Baz % %======*======*======*======*