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
194 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans

630 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