Link to home
Start Free TrialLog in
Avatar of vctsang
vctsang

asked on

Use Perl to print left justified for 9 columns sepearte by a space and line them up from left to right

These are stings and some fields have more characters than the other.  I want to print the text file separate by a space and left justified. I should have an output that the fields from each columns lined up left justifed.

Thanks,

-Vin

Avatar of rj2
rj2

Could you post a sample of the text file and how you want to print it?
Avatar of vctsang

ASKER

There 9 columes in the test and I can not fit it in this screen and it will wrap around. I want to line the 2nd testserv1 rows with #System rows to left justified.

#SYSTEM System_process_name test_pro_name ... ...
testsev1 test_proc1 dacserver
#SYSTEM System_process_name test_pro_name ... ...
testsev1 test_proc12 dacservers

Output;
#SYSTEM  System_process_name test_pro_name ... ...
testsev1 test_proc1          dacserver     ... ...
#SYSTEM  System_process_name test_pro_name ... ...
testsev1 test_proc12         dacservers    ... ...
Avatar of vctsang

ASKER



Output;
#SYSTEM   System_process_name  test_pro_name ... ...
testsev1  test_proc1           dacserver     ... ...
#SYSTEM   System_process_name test_pro_name ... ...
testsev1  test_proc12         dacservers    ... ...
ASKER CERTIFIED SOLUTION
Avatar of rj2
rj2

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
open(FILE,"temp.txt") || die "Could not open file: $!\n";

while(<FILE>) {
    $i = 0;
    foreach (split) {
        printf "Field $_.  Comparing $max[$i] and %s.  ", length($_) +1;
        $max[$i++] = ((length($_) +1) > $max[$i] ) ? length($_) +1 : $max[$i];
        print "Got $max[$i -1]\n";
    }                    
}

seek(FILE,0,0);
while(<FILE>) {    
    $i = 0;
    foreach (split) {
         printf "%-$max[$i++]s", $_;
    }
    print "\n";
}
Whoops - Left some debug in.
Avatar of vctsang

ASKER

r2j,
      Thanks for the script and it works.  How do I make the text file as the argument so I can run it with different file name.  

Thanks,

-Vin
Just replace the line
my($file)='e:\inetpub\scripts\table.txt';
with
my($file)=$ARGV[0];