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
Thanks,
-Vin
Could you post a sample of the text file and how you want to print it?
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 ... ...
#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 ... ...
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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";
}
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.
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
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\scri pts\table. txt';
with
my($file)=$ARGV[0];
my($file)='e:\inetpub\scri
with
my($file)=$ARGV[0];