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
182 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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