Thanks Rob,

The lines get broken when for example, /u01/app/oracle/admin/db01/adump
becomes /u01/app/oracle/admin/db1234/adump or
/u01/app/oracle/admin/db01/diagnosis and in most cases, I will manually
edit the file to line them up.

I know it is mostly for aesthetic purpose lining them up. Your sample code
below is a big help. Thanks again.

On Sun, Sep 16, 2012 at 12:28 AM, Rob Dixon <rob.di...@gmx.com> wrote:

> On 15/09/2012 02:52, newbie01 perl wrote:
> > Hi all,
> >
> > I have a config file that contains some text as below:
> >
> > server01        :/u01/app/oracle/admin/db01/adump              :*.dmp,5
> >     :compress         :/bin/gzip
> > server01        :/u01/app/oracle/admin/db04/adump             :*.aud,5
> >     :remove         :/bin/rm
> > server01        :/u01/app/oracle/admin/db06/adump              :*.log,5
> >     :remove         :logrotate
> >
> > But am wanting to run a script that will take this file as an input and
> > produced the following output:
> >
> > server01        :/u01/app/oracle/admin/db01/adump              :*.dmp,5
> >     :compress       :/bin/gzip
> > server01        :/u01/app/oracle/admin/db04/adump              :*.aud,5
> >     :remove         :/bin/rm
> > server01        :/u01/app/oracle/admin/db06/adump              :*.log,5
> >     :remove         :logrotate
> >
> > Can anyone advise how is the best way to be able to format the text so
> that
> > they lined up neatly? Perhaps anyone already have a script that does what
> > am wanting to do?
> >
> > At the moment, am doing it manually reading one line at a time and using
> > awk and printf using the max string of each column as the basis for the
> max
> > length of the column for each field. Am wanting to be able to use Perl
> > instead.
> >
> > Any feedback much appreciated. Thanks in advance.
>
> I assume the data has been wrapped by your email client and the lines
> aren't in fact broken after the third field?
>
> I suggest you read through the data, collecting the fields from each
> record and keeping a maximum width of each column. Then you can build a
> format string dynamically and use it to print each record of the
> original data.
>
> The program below should give you a start.
>
> HTH,
>
> Rob
>
>
>
> use strict;
> use warnings;
>
> my @data;
> my @widths;
>
> while (<DATA>){
>   my @fields = split;
>   my $i = 0;
>   for my $field (@fields) {
>     my $len = length $field;
>     $widths[$i] = $len unless $widths[$i] and $widths[$i] >= $len;
>     $i++;
>   }
>   push @data, \@fields;
> }
>
> my $format = join(' ', map "%-${_}s", @widths)."\n";
> printf $format, @$_ for @data;
>
> __DATA__
> server01        :/u01/app/oracle/admin/db01/adump              :*.dmp,5
> :compress         :/bin/gzip
> server01        :/u01/app/oracle/admin/db04/adump             :*.aud,5
> :remove         :/bin/rm
> server01        :/u01/app/oracle/admin/db06/adump              :*.log,5
> :remove         :logrotate
>
> **output**
>
> server01 :/u01/app/oracle/admin/db01/adump :*.dmp,5 :compress :/bin/gzip
> server01 :/u01/app/oracle/admin/db04/adump :*.aud,5 :remove   :/bin/rm
> server01 :/u01/app/oracle/admin/db06/adump :*.log,5 :remove   :logrotate
>
>

Reply via email to