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 -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/