Re: script help - stripping trailing spaces in exisitng script
- From: Gunnar Hjalmarsson <noreply@xxxxxxxxx>
- Date: Mon, 06 Jun 2005 05:06:26 +0200
Greg wrote:
Can some give me a hand to simplify a perl script? I currently run the below script on the below sample data. The only problem is the trailing spaces at the end of each line. A quick web search put me onto "s/\s+$//" which I was easily able to add into another script that runs on the data after the first one is finished with it and cleans up these spaces perfectly.
This just seems very kludgy, can someone tell me how to change the original to just include all this in a single script.
<snip>
printf(FILE2"%-9s%-8s%-80s\n",$col[2],$col[0],$col1[$i]);
------------------------^^^ Exchange that %-80s for %s. Same thing for next printf() statement.
The whole script can probably be simplified to something like:
use strict;
use warnings;
my $file1 = "$ENV{TEMP}/65.txt";
my $file2 = "$ENV{TEMP}/65_2.txt";
open my $F1, $file1 or die "Can't open $file1: $!";
open my $F2, "> $file2" or die "Can't create $file2: $!";
while (<$F1>) {
chomp;
my @col = split /@/;
if ( $col[1] and $col[1] ne '^(DS)' ) {
my @col1 = split /_/, $col[1];
printf $F2 "%-9s%-8s%s\n", ($col[2] or ''), $col[0], $col1[0];
printf $F2 "%-9s%-8s%s\n", '', '', $col1[$_] for 1..$#col1;
}
}-- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl .
- Prev by Date: Re: Possible bug in 5.8.6: accept/fork/wait/exit ?
- Next by Date: FAQ 9.17 How do I check a valid mail address?
- Previous by thread: Possible bug in 5.8.6: accept/fork/wait/exit ?
- Next by thread: FAQ 9.17 How do I check a valid mail address?
- Index(es):
Relevant Pages
|