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

T

Thomas 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
 
P

patrik.nyman

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
 
P

patrik.nyman

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
 
P

Paul Lalli

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top