flood
April 8, 2009, 10:31am
1
I am new to Perl. I need to reformat a data file as the last part of a script I am working on. I am stuck on this.
Here is the current format:
CUSTOMER Filename 09/04/07-08:49
CUSTOMER Filename 09/04/07-08:52
CUSTOMER Filename 09/04/07-08:52
CUSTOMER2 Filename 09/04/07-08:49
CUSTOMER2 Filename 09/04/07-08:52
CUSTOMER2 Filename 09/04/07-08:52
I need it to look like this:
CUSTOMER
Filename 09/04/07-08:49
Filename 09/04/07-08:52
Filename 09/04/07-08:52
CUSTOMER2
Filename 09/04/07-08:49
Filename 09/04/07-08:52
Filename 09/04/07-08:52
Any ideas? I've thought about looping thru the list and storing each list item as a variable and then printing the variables, but I haven't worked out how exactly to do it.
standard key holding mechanism:
#!/usr/local/bin/perl
$fin = "c";
unless ( open FIN, $fin ){
print "cannot read file $fin \n";
exit(9);
}
@a_lines = <FIN>; chomp @a_lines; close FIN;
$prev_customer = "ZZZZ";
foreach $line ( @a_lines ){
( $customer, @a_junk ) = split( / /, $line );
if ( $prev_customer ne $customer ){
if ( $prev_customer ne "ZZZZ" ){
print "\n";
}
$prev_customer = $customer;
print "$customer\n";
}
print join ' ', @a_junk;
print "\n";
}
same solution in ksh:
#!/bin/ksh
prev_customer=ZZZZ
cat c |
while read customer junk ; do
if [ $prev_customer != $customer ]; then
if [ $prev_customer != "ZZZZ" ]; then
print
fi
prev_customer=$customer
print $customer
fi
print $junk
done
was stuck in perl mode, i guess...
flood
April 8, 2009, 11:21am
4
quirkasaurus, that works great.
I've never used a key holding mechanism so thanks. I am sure I will find a lot of uses for it going forward. I really appreciate your help.