script help - stripping trailing spaces in exisitng script

G

Greg

Hi,

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.

Thanks in advance


Sample Data
@@
F6@@2A04
F7@@2A04
F8@@2A04
@@2A04
A10:1@@2A25
A16@INSTRUMENTATION MODULE (INCORPORATES S4)_SEE 1691-A3 SHT B10@2A25
A18@SYSTEM VOLTAGE DETECTOR "CABLE ALIVE" INDICATION@2A25
A2@@2A25
A20@SICM - SERIAL INTERFACE CONTROL MODULE_XXX TYPE - SEE
PRS02-RS014-01@2A25
A2:1@@2A25
A2:10@@2A25
A2:11@@2A25
A2:12@@2A25
A2:13@@2A25
A2:17@^(DS)@2A25
A2:18@^(DS)@2A25
A2:19@^(DS)@2A25
A2:2@@2A25
A2:20@^(DS)@2A25
A2:3@@2A25
A2:4@@2A25
A2:5@@2A25
A2:6@@2A25
A2:7@@2A25
A2:8@@2A25
A2:9@@2A25
F10@FUSE VT SECONDARY@2A25
F11@FUSE VT SECONDARY@2A25
F9@FUSE VT SECONDARY@2A25
J32@RELAY - 3DOC DEF & 3OC EF PROT_SEL TYPE 351 MODEL 3516144563XX1_SEE
PRS01-RW001-03@2A25
P1@AMMETER CROMPTON INSTRUMENTS TYPE 244-01AG@2A25
P2@VOLTMETER CROMPTON INSTRUMENTS TYPE 244-02VG@2A25
P2@@2A25
S4@AMMETER SELECTOR SWITCH (PART OF A16)_SEE 1691-A3 SHT B10@2A25
S5@VOLTMETER SELECTOR SWITCH, K & N TYPE AU2 R20@2A25
T1@INTERPOSING CT 5A/5mA@2A25
T2@INTERPOSING CT 5A/5mA@2A25
T3@INTERPOSING CT 5A/5mA@2A25
X11@LINK ISOLATION EXTERNAL OUTPUTS A_MOUNTED IN INSTRUMENT
CHAMBER@2A25
X12@LINK ISOLATION EXTERNAL OUTPUTS B_MOUNTED IN INSTRUMENT
CHAMBER@2A25
X13@LINK ISOLATION EXTERNAL OUTPUTS C_MOUNTED IN INSTRUMENT
CHAMBER@2A25
@@2A25
A2:14@@
A2:15@@
A2:16@@
A2:21@@
@MULTICORE CABLE CONNECTION BLOCK@
@@


Existing perl script

# Define files.
$file1="$ENV{'TEMP'}\\65.txt";
$file2="$ENV{'TEMP'}\\65_2.txt";

# Open result file form the select statement, for reading.
open (FILE1, "<$file1") || die "Can't open: $!\n";

# Open temporary result file, for writing.
open (FILE2, ">$file2") || die "Can't create: $!\n";

$count = 0;
$count1 = 0;

# Read row by row in file.
while (<FILE1>)
{
# Split file in colums.
chomp;
@col=split('@');

if ($col[0] ne "")
{
if ($col[1] ne "^(DS)")
{
@col1 = split('\_', $col[1]);
$count1 = scalar @col1;

for ($i=0; $i<$count1; $i++ )
{
if ($i eq 0 )
{

printf(FILE2"%-9s%-8s%-80s\n",$col[2],$col[0],$col1[$i]);
} else
{
printf(FILE2"%-9s%-8s%-80s\n","","",$col1[$i]);
}
}
}
}
}

printf(FILE2" \n");

# Close files.
close FILE1; close FILE2; # Close files.
 
G

Gunnar Hjalmarsson

Greg said:
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.

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;
}
}
 

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top