#!/usr/bin/perl -w use strict; my $file = "question.lst"; my $newfile = "question2.lst"; my @newlines; open(FILE, "<$file") || die "$!\n"; chomp(my @lines = ); close FILE; foreach(@lines) { s/\t/,/g; # substitute commas for any tabs # This keeps the field delimiters all the same # If your data is going to have tabs in it, # remove that line my @elements = split(/,/, $_); # since the split splits the last and first name, # and the "City, State" fields # join them back together and remove the unnecessary # elements $elements[0] = "$elements[0],$elements[1]"; $elements[4] = "$elements[4], $elements[5]"; splice(@elements, 1, 1); splice(@elements, 4, 1); # If the elements aren't already enclosed in quotes, # enclose them. foreach(@elements) { $_ = qq("$_") unless (m/\".*\"/); } # Join the elements of the line back together # and push them to a new array my $newline = join(",", @elements); push(@newlines, $newline); } # Print the information to a new file open(NEWFILE, ">$newfile") || die "$!\n"; foreach(@newlines) {print NEWFILE "$_\n";} close NEWFILE;