#!/usr/bin/env perl -l use strict; use warnings; use Text::CSV; my $csv = Text::CSV::->new; while () { $csv->print(\*STDOUT, [ map { /^\s*(.*?)\s*$/ } unpack 'A3A9A9A*' ]); } __DATA__ 123 qwe, rty 04/05/06 X 456 asd, fgh 07/08/09 XX 789 zxc, vbn 01/02/03 XXX #### 123,"qwe, rty",04/05/06,X 456,"asd, fgh",07/08/09,XX 789,"zxc, vbn",01/02/03,XXX