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 ?

Post by t.blab » Sat, 17 Nov 2007 22:49:05


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
 
 
 

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

Post by patrik.nym » Sat, 17 Nov 2007 23:11:28


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.yqcomputer.com/

Cheers,
/Patrik Nyman

 
 
 

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

Post by patrik.nym » Sat, 17 Nov 2007 23:15:13


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
 
 
 

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

Post by Paul Lall » Sat, 17 Nov 2007 23:32:52


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
 
 
 

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

Post by Petr Vilet » Sat, 17 Nov 2007 23:37:20


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 = <IN>)
{
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 = <IN>)
{
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 = <IN>)
{
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.)
 
 
 

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

Post by bugbea » Sun, 18 Nov 2007 01:11:41


Beautiful. Direct and concise without being cryptic.

BugBear