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

Discussion in 'Perl Misc' started by Thomas Blabb, Nov 16, 2007.

  1. Thomas Blabb

    Thomas Blabb Guest

    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
    Thomas Blabb, Nov 16, 2007
    #1
    1. Advertising

  2. Thomas Blabb

    Guest

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

    On 16 Nov, 14:49, (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
    , Nov 16, 2007
    #2
    1. Advertising

  3. Thomas Blabb

    Guest

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

    On 16 Nov, 15:11, wrote:
    > On 16 Nov, 14:49, (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
    , Nov 16, 2007
    #3
  4. Thomas Blabb

    Paul Lalli Guest

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

    On Nov 16, 8:49 am, (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
    Paul Lalli, Nov 16, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nobody
    Replies:
    5
    Views:
    601
    Jukka K. Korpela
    Dec 14, 2004
  2. Replies:
    5
    Views:
    1,103
    Ian Rastall
    Jun 29, 2005
  3. =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=

    Left justified or center justified under XSL:FO

    =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=, Feb 9, 2007, in forum: XML
    Replies:
    0
    Views:
    482
    =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=
    Feb 9, 2007
  4. Catherina Svensson
    Replies:
    0
    Views:
    301
    Catherina Svensson
    Apr 29, 2008
  5. Ben Thomas
    Replies:
    7
    Views:
    261
    Jan Svitok
    Nov 16, 2006
Loading...

Share This Page