#!/usr/bin/env perl -l use strict; use warnings; my $re = qr{ ^ # start of line (\S+)\s+(\S+)\s+(\S+)\s+ # 3 fields, no spaces (.+?)\s+ # 1 field, +/- spaces (\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+) # 7 fields, no spaces \s* # possible spaces ($) # end of line }x; print join ';' => /$re/ while ; __DATA__ PID POLS U(%) POOL_NAME Seq# Num LDEV# H(%) VCAP(%) TYPE PM 003 POLN 0 Bad name with spaces 13453 2 61443 80 - OPEN N 002 POLN 52 DemoSolutions 54068 7 61454 80 - OPEN N #### PID;POLS;U(%);POOL_NAME;Seq#;Num;LDEV#;H(%);VCAP(%);TYPE;PM; 003;POLN;0;Bad name with spaces;13453;2;61443;80;-;OPEN;N; 002;POLN;52;DemoSolutions;54068;7;61454;80;-;OPEN;N;