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
177 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now