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
179 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

910 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

22 Experts available now in Live!

Get 1:1 Help Now