# for each line, # get fields you're keeping, put them into @fields # in the proper order foreach my $field (@fields) { $field =~ s/^\s*(.*?)\s*$/; # trim whitespace -- but beware! # two-command version (see the FAQ in perlfaq) # $field =~ s/^\s*//; # $field =~ s/\s*$//; } print OUTPUTFILEHANDLE join "|" @fields; # now process the next line