Solved

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

Posted on 2002-05-30
8
190 Views
Last Modified: 2008-03-17
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

0
Comment
Question by:vctsang
  • 3
  • 3
  • 2
8 Comments
 
LVL 10

Expert Comment

by:rj2
ID: 7044896
Could you post a sample of the text file and how you want to print it?
0
 

Author Comment

by:vctsang
ID: 7045035
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    ... ...
0
 

Author Comment

by:vctsang
ID: 7045038


Output;
#SYSTEM   System_process_name  test_pro_name ... ...
testsev1  test_proc1           dacserver     ... ...
#SYSTEM   System_process_name test_pro_name ... ...
testsev1  test_proc12         dacservers    ... ...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Accepted Solution

by:
rj2 earned 200 total points
ID: 7047524
#!/usr/bin/perl
use strict;

my($file)='e:\inetpub\wwwroot\data.txt';
open(FILE,$file) || die("Could not open file $file because $!");

my($cols)=9;
my(@maxlength,@line,$i);
while(<FILE>) {
     chomp;
     @line=split(' ',$_,$cols);
     for($i=0;$i<$cols;$i++) {
          my($l)=length($line[$i]);
          if($l > $maxlength[$i]) {
               $maxlength[$i]=$l;
          }
     }                    
}
seek(FILE,0,0);

while(<FILE>) {    
     chomp;    
     @line=split(' ',$_,$cols);
     for($i=0;$i < $cols; $i++) {
          my($spacecount)=$maxlength[$i] - length($line[$i]) + 1;
          if($i == $cols-1) {
               print $line[$i],"\n";
          } else {
               print $line[$i],' ' x $spacecount;
          }
     }    
}
0
 
LVL 5

Expert Comment

by:PC_User321
ID: 7048348
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";
}
0
 
LVL 5

Expert Comment

by:PC_User321
ID: 7048352
Whoops - Left some debug in.
0
 

Author Comment

by:vctsang
ID: 7078181
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
0
 
LVL 10

Expert Comment

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
grep that displays 4 lines above & 1 line below of what's found 10 85
Perl passing in variables to do substitution 6 78
Perl Frameworks 1 107
perl getopt long help 34 84
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question