How to re-write a text files content to have right-justified columns ?

How to re-write a text files content to have right-justified columns ?

am 16.11.2007 14:49:05 von t.blabb

Assume I have a text file where each lines contains at first a number and then
some other stuff in the rest of the line. The rest of the line is delimited by a space e.g.


14412 blah blah
23 sometext2
34252346 rest text with multiple words


I want to convert this text file so that the numbers are right justified in a column of width lets say 10:


14412 blah blah
23 sometext2
34252346 rest text with multiple words

The second part of each line should be left-justified (as before)

How can I do this with perl?
How would a perl script looks like ?

Thank you for the answer to a perl newbie

Tom

Re: How to re-write a text files content to have right-justified

am 16.11.2007 15:11:28 von patrik.nyman

On 16 Nov, 14:49, t.bl...@gmail.com (Thomas Blabb) wrote:
> Assume I have a text file where each lines contains at first a number and then
> some other stuff in the rest of the line. The rest of the line is delimited by a space e.g.
>
> 14412 blah blah
> 23 sometext2
> 34252346 rest text with multiple words
>
> I want to convert this text file so that the numbers are right justified in a column of width lets say 10:
>
> 14412 blah blah
> 23 sometext2
> 34252346 rest text with multiple words
>
> The second part of each line should be left-justified (as before)
>
> How can I do this with perl?
> How would a perl script looks like ?

It could look like this:

---------------------%<----------------------------
#!perl
format MYFORMAT =
@>>>>>>>>>>
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$numbers, $the_rest
..
$~="MYFORMAT";
while (<>) {
next if /^\s*$/;
if (/(\d+)\s(.*)/) {
$numbers = $1;
$the_rest = $2;
}
write;
}
--------------------%<--------------------------

A good introduction to formating can be found at
http://www.webreference.com/programming/perl/format/

Cheers,
/Patrik Nyman

Re: How to re-write a text files content to have right-justified

am 16.11.2007 15:15:13 von patrik.nyman

On 16 Nov, 15:11, patrik.ny...@orient.su.se wrote:
> On 16 Nov, 14:49, t.bl...@gmail.com (Thomas Blabb) wrote:
>
>
>
> > Assume I have a text file where each lines contains at first a number and then
> > some other stuff in the rest of the line. The rest of the line is delimited by a space e.g.
>
> > 14412 blah blah
> > 23 sometext2
> > 34252346 rest text with multiple words
>
> > I want to convert this text file so that the numbers are right justified in a column of width lets say 10:
>
> > 14412 blah blah
> > 23 sometext2
> > 34252346 rest text with multiple words
>
> > The second part of each line should be left-justified (as before)
>
> > How can I do this with perl?
> > How would a perl script looks like ?
>
> It could look like this:
>
> ---------------------%<----------------------------
> #!perl
> format MYFORMAT =
> @>>>>>>>>>>
> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
> $numbers, $the_rest
> .
> $~="MYFORMAT";
> while (<>) {
> next if /^\s*$/;
> if (/(\d+)\s(.*)/) {
> $numbers = $1;
> $the_rest = $2;
> }
> write;}
>
> --------------------%<--------------------------
>
> A good introduction to formating can be found athttp://www.webreference.com/programming/perl/format/
>
> Cheers,
> /Patrik Nyman

Ooops, a line break got inserted by the mailer.
The first line of MYFORMAT should be
@>>>>>>>>>> @<<<<<< ....
Put the two lines together in your editor before running the script.

/Patrik

Re: How to re-write a text files content to have right-justified

am 16.11.2007 15:32:52 von Paul Lalli

On Nov 16, 8:49 am, t.bl...@gmail.com (Thomas Blabb) wrote:
> Assume I have a text file where each lines contains at first a number and then
> some other stuff in the rest of the line. The rest of the line is delimited by a space e.g.
>
> 14412 blah blah
> 23 sometext2
> 34252346 rest text with multiple words
>
> I want to convert this text file so that the numbers are right justified in a column of width lets say 10:
>
> 14412 blah blah
> 23 sometext2
> 34252346 rest text with multiple words
>
> The second part of each line should be left-justified (as before)
>
> How can I do this with perl?
> How would a perl script looks like ?

perldoc -f sprintf
perldoc -f printf

$ cat data.txt
14412 blah blah
23 sometext2
34252346 rest text with multiple words

$ perl -lne'
my ($num, $rest) = split / /, $_, 2;
printf("%10s %s\n", $num, $rest);
' data.txt
14412 blah blah
23 sometext2
34252346 rest text with multiple words


Paul Lalli

Re: How to re-write a text files content to have right-justified columns ?

am 16.11.2007 15:37:20 von Petr Vileta

Thomas Blabb wrote:
> Assume I have a text file where each lines contains at first a number
> and then
> some other stuff in the rest of the line. The rest of the line is
> delimited by a space e.g.
>
>
> 14412 blah blah
> 23 sometext2
> 34252346 rest text with multiple words
>
>
> I want to convert this text file so that the numbers are right
> justified in a column of width lets say 10:
>
>
> 14412 blah blah
> 23 sometext2
> 34252346 rest text with multiple words
>
> The second part of each line should be left-justified (as before)
>
> How can I do this with perl?
> How would a perl script looks like ?
>
First you must determine the maximal length of number, so you must read
whole file line by line. If your file is small then you can store numbers
and texts to array for further use, but is your file is huge then you can't
use array.

# example for small file

my $file = 'c:/files/textfile.txt';
# or on unix like system
# my $file = '/var/files/textfile.txt';
my $newfile = $file;
$newfile =~ s/^(.+)$/new_$1/;
my $maxlen = 0;
my @rows;
open IN, "< $file" or die "Can't open file $file";
while (my $row = )
{
my ($number, $text) = split(/\s+/, $row);
my $len = length($number);
$maxlen = $len if($len > $maxlen);
push @rows, [$number, $text];
}
close IN;
open OUT, "> $newfile" or die "Can't create file $newfile";
foreach my $row (@rows)
{
print OUT ' 'x($maxlen - length($row->[0]), $row->[0], ' ', $row->[1];
}
close OUT;


# example for huge file

my $file = 'c:/files/textfile.txt';
# or on unix like system
# my $file = '/var/files/textfile.txt';
my $newfile = $file;
$newfile =~ s/^(.+)$/new_$1/;
my $maxlen = 0;
my @rows;
open IN, "< $file" or die "Can't open file $file";
while (my $row = )
{
my ($number, $text) = split(/\s+/, $row);
my $len = length($number);
$maxlen = $len if($len > $maxlen);
}
close IN;
open IN, "< $file";
open OUT, "> $newfile" or die "Can't create file $newfile";
while (my $row = )
{
my ($number, $text) = split(/\s+/, $row);
my $len = $maxlen - length($number);
print OUT ' 'x$len, $number, ' ', $text;
}
close IN;
close OUT;

--

Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)

Re: How to re-write a text files content to have right-justified columns ?

am 16.11.2007 17:11:41 von bugbear

Paul Lalli wrote:
> On Nov 16, 8:49 am, t.bl...@gmail.com (Thomas Blabb) wrote:
>> Assume I have a text file where each lines contains at first a number and then
>> some other stuff in the rest of the line. The rest of the line is delimited by a space e.g.
>>
>> 14412 blah blah
>> 23 sometext2
>> 34252346 rest text with multiple words
>>
>> I want to convert this text file so that the numbers are right justified in a column of width lets say 10:
>>
>> 14412 blah blah
>> 23 sometext2
>> 34252346 rest text with multiple words
>>
>> The second part of each line should be left-justified (as before)
>>
>> How can I do this with perl?
>> How would a perl script looks like ?
>
> perldoc -f sprintf
> perldoc -f printf
>
> $ cat data.txt
> 14412 blah blah
> 23 sometext2
> 34252346 rest text with multiple words
>
> $ perl -lne'
> my ($num, $rest) = split / /, $_, 2;
> printf("%10s %s\n", $num, $rest);
> ' data.txt
> 14412 blah blah
> 23 sometext2
> 34252346 rest text with multiple words
>
>
> Paul Lalli
>

Beautiful. Direct and concise without being cryptic.

BugBear