#!/usr/bin/perl -w use strict; # Loop through input filenames listed on command line foreach my $inpfnm (@ARGV) { # Try to open the file for read (input) if (!open INPFIL, "<$inpfnm") { print "ERROR: Cannot open input file '$inpfnm'\n"; } else { # Derive the output filename my $outfnm = $inpfnm . '-out.dat'; # Try to open the file for write (output) if (!open OUTFIL, ">$outfnm") { print "ERROR: Cannot open output file '$outfnm'\n"; } else { # Loop through lines from the input file while (my $inpbuf = ) { # Remove the newline at the end of the line chomp $inpbuf; # Split input by vertical bar (pipe) character my @inpelt = split /\|/, $inpbuf; # Go through each element from the split foreach my $inpelt (@inpelt) { if ($inpelt !~ /^\s*$/) { # Line is not blank. Proceed. # Prepare output buffer. my $outbuf = $inpelt; # Trim leading and trailing whitespace $outbuf =~ s/^\s+//; $outbuf =~ s/\s+$//; # Write output print OUTFIL "$outbuf\n"; } } } # Cleanup close OUTFIL; } # Cleanup close INPFIL; } } exit; __END__