php -q fixed_column_split.php < inputfile.txt > outputfile.txt # # # You must first change the sp_data_definition() function # -------------------- # to match your input data. You will need the key to translate # the data first. # define('DELIMITER',","); # What to split fields by. Usually a tab ("\t") # or a comma (","). define('ENCAPSULATOR','"'); # Do you want to wrap each field in something? # A quote maybe... define('EOL',"\n"); # Unix end-of-line is the \n (newline) char. # Other options are \r\n (DOS) \r (MAC) define('OFFSET',1); # if the "start byte" is 1, then set this... # else, set it to 0. # Define the fixed length data here. # # There are three fields. # - First field is a NAME (can be anything except a pipe (|). # - Second field is the start column. # - Third field is the column length. # Separete by a pipe (|). function sp_data_definition() { return array( 'county code|1|3', 'reg #|4|8', 'votes status|12|1', 'name l|13|20', 'name f|33|20', 'name m|53|20', 'suffix|73|3', 'title|76|3', 'rs address #|79|6', 'street name|85|30', 'rcs. street suffix|115|2', 'apt #|117|8', 'city|125|17', 'zip|142|9', 'flag mail|151|2', 'transsomething...|153|1', 'transea?...|154|6', 'filler..|160|2', 'DOB|162|8', 'reg date|170|8', 'race|178|1', 'gender|179|1', 'filler|180|1', 'land district|181|3', 'land lot|184|4', 'old reg date|188|8', 'reason status|196|9', 'filler|205|5', 'county presinct ID|210|5', 'city precinct ID|215|5', 'voter district|220|166', 'date last voted|386|8', 'type of election|394|3', 'party last elected|397|3', ); } ################################################################## ## You should not need to edit anything below here ################################################################## function sp_read_stdin_line($line_length=4096) { $buf = ''; $fd = fopen('php://stdin', 'r'); if ($fd) { $buf = fgets($fd, $line_length); fclose($fd); } if (strlen($buf)) return $buf; } function sp_stderr($buf) { $fd = fopen('php://stderr','w'); if ($fd) { fwrite($fd, $buf, strlen($buf)); fclose($fd); } } set_time_limit(0); function sp_do_procedure($buf) { static $done_label; $object = sp_data_definition(); reset($object); $newline = ''; $label_line = ''; $out = ''; while(list(,$obj_row) = each($object)) { list($label,$start_char,$char_len) = split('\|',$obj_row,3); $start_char -= OFFSET; $newline .= ENCAPSULATOR.trim(substr($buf,$start_char,$char_len)).ENCAPSULATOR.DELIMITER; if (!$done_label) { $label_line .= ENCAPSULATOR.trim($label).ENCAPSULATOR.DELIMITER; } } if (!$done_label) { $done_label = TRUE; $label_line = substr($label_line, 0, strlen($label_line)-1); print $label_line . EOL; } $newline = substr($newline, 0, strlen($newline)-1); print $newline . EOL; } $fd = fopen('php://stdin','r'); if ($fd) { while($line = fgets($fd, 10000)) { sp_do_procedure($line); } fclose($fd); } else { sp_stderr("No stdin. Exiting.\n"); } ?>