Add columns of a File

Discussion in 'Perl Misc' started by Rajpreet, Jan 14, 2009.

  1. Rajpreet

    Rajpreet Guest

    Greetings,

    I am fairly new to perl. I have a sample file and need to add file 4th
    and 5th column and create a column with this new value. Something like
    below

    Sample File:

    123456 12345 1234 123
    12 135 ---- This is addition of 4th and 5th column
    789 789 789
    78 7 85

    I need to either put the new column in the same file or create a new
    file with the above format. Any idea?

    TIA
     
    Rajpreet, Jan 14, 2009
    #1
    1. Advertising

  2. Rajpreet <> wrote:

    > I am fairly new to perl.



    Have you seen the Posting Guidelines that are posted here frequently?


    > I have a sample file and need to add file 4th
    > and 5th column and create a column with this new value. Something like
    > below
    >
    > Sample File:
    >
    > 123456 12345 1234 123
    > 12 135 ---- This is addition of 4th and 5th column
    > 789 789 789
    > 78 7 85



    You should disable word-wrap in your newsreader then you have
    long lines of data to post...


    > I need to either put the new column in the same file



    perl -pi -e 's/((\d+)\s+(\d+))$/ "$1 " . ($2 + $3) /e' data.file


    > or create a new
    > file with the above format.



    perl -pe 's/((\d+)\s+(\d+))$/ "$1 " . ($2 + $3) /e' data.file >new.file


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Jan 14, 2009
    #2
    1. Advertising

  3. Rajpreet

    Rajpreet Guest

    On Jan 14, 6:21 pm, Tad J McClellan <> wrote:
    > Rajpreet <> wrote:
    > > I am fairly new to perl.

    >
    > Have you seen the Posting Guidelines that are posted here frequently?
    >
    > > I have a sample file and need to add file 4th
    > > and 5th column and create a column with this new value. Something like
    > > below

    >
    > > Sample File:

    >
    > > 123456         12345              1234             123
    > > 12        135 ---- This is addition of 4th and 5th column
    > >  789               789                  789
    > > 78              7          85

    >
    > You should disable word-wrap in your newsreader then you have
    > long lines of data to post...
    >
    > > I need to either put the new column in the same file

    >
    >     perl -pi -e 's/((\d+)\s+(\d+))$/ "$1   " . ($2 + $3) /e' data.file
    >
    > > or create a new
    > > file with the above format.

    >
    >     perl -pe 's/((\d+)\s+(\d+))$/ "$1   " . ($2 + $3) /e' data.file>new.file
    >
    > --
    > Tad McClellan
    > email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


    Thanks Tad. I will try that and will come back in case of issues.
    Thanks again
     
    Rajpreet, Jan 14, 2009
    #3
  4. Rajpreet

    test Guest

    On Jan 14, 7:08 pm, Rajpreet <> wrote:
    > On Jan 14, 6:21 pm, Tad J McClellan <> wrote:
    >
    >
    >
    >
    >
    > > Rajpreet <> wrote:
    > > > I am fairly new to perl.

    >
    > > Have you seen the Posting Guidelines that are posted here frequently?

    >
    > > > I have a sample file and need to add file 4th
    > > > and 5th column and create a column with this new value. Something like
    > > > below

    >
    > > > Sample File:

    >
    > > > 123456         12345              1234             123
    > > > 12        135 ---- This is addition of 4th and 5th column
    > > >  789               789                  789
    > > > 78              7          85

    >
    > > You should disable word-wrap in your newsreader then you have
    > > long lines of data to post...

    >
    > > > I need to either put the new column in the same file

    >
    > >     perl -pi -e 's/((\d+)\s+(\d+))$/ "$1   " . ($2 + $3) /e' data..file

    >
    > > > or create a new
    > > > file with the above format.

    >
    > >     perl -pe 's/((\d+)\s+(\d+))$/ "$1   " . ($2 + $3) /e' data.file >new.file

    >
    > > --
    > > Tad McClellan
    > > email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

    >
    > Thanks Tad. I will try that and will come back in case of issues.
    > Thanks again- Hide quoted text -
    >
    > - Show quoted text -


    In Addition, can you please let me know how the command is actually
    working. I am sorry but it is a bit hard to understand the flow here?
     
    test, Jan 14, 2009
    #4
  5. Rajpreet <> wrote:


    >> --
    >> Tad McClellan
    >> email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"



    Please do not quote .sigs.


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Jan 14, 2009
    #5
  6. test <> wrote:
    > On Jan 14, 7:08 pm, Rajpreet <> wrote:
    >> On Jan 14, 6:21 pm, Tad J McClellan <> wrote:



    >> >     perl -pi -e 's/((\d+)\s+(\d+))$/ "$1   " . ($2 + $3) /e' data.file



    > In Addition, can you please let me know how the command is actually
    > working.



    You can look up the various parts in Perl's std docs and come back
    with any questions that remain after that.

    The -p -i and -e switches:

    perldoc perlrun

    The ((\d+)\s+(\d+))$ pattern:

    perldoc perlrequick
    perldoc perlretut
    perldoc perlre

    The s///e operator:

    perldoc perlop


    > I am sorry but it is a bit hard to understand the flow here?


    Once you know what the switches are doing, convert from a one-liner
    to a regular program in a file.

    while ( <> ) {
    s/((\d+)\s+(\d+))$/ "$1   " . ($2 + $3) /e;
    print;
    }


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Jan 14, 2009
    #6
  7. Rajpreet

    cartercc Guest

    On Jan 14, 7:23 am, Rajpreet <> wrote:
    > Greetings,
    >
    > I am fairly new to perl. I have a sample file and need to add file 4th
    > and 5th column and create a column with this new value. Something like
    > below
    >
    > Sample File:
    >
    > 123456         12345              1234             123
    > 12        135 ---- This is addition of 4th and 5th column
    >  789               789                  789
    > 78              7          85
    >
    > I need to either put the new column in the same file or create a new
    > file with the above format. Any idea?
    >
    > TIA


    You don't specify what other processing you are doing or how you are
    parsing the file. Assuming that you use an array (say, @row) to
    manipulate the values of each line, you can do something like this:

    printf OUTFILE "$row[0]\t$row[1]\t$row[2]\t$row[3]\t$row[4]\t%d\n",
    $row[3] + $row[4];

    This formats a print string and prints it to your outfile, adding the
    values for array indexes 3 and 4 (fourth and fifth columns). Check
    both printf and sprintf.

    CC
     
    cartercc, Jan 14, 2009
    #7
  8. Rajpreet wrote:
    >
    > I am fairly new to perl. I have a sample file and need to add file 4th
    > and 5th column and create a column with this new value. Something like
    > below
    >
    > Sample File:
    >
    > 123456 12345 1234 123
    > 12 135 ---- This is addition of 4th and 5th column
    > 789 789 789
    > 78 7 85
    >
    > I need to either put the new column in the same file


    perl -i -lape'$_.="\t".($F[3]+$F[4])' yourfile


    > or create a new file with the above format.


    perl -lape'$_.="\t".($F[3]+$F[4])' yourfile > new_file



    John
    --
    Those people who think they know everything are a great
    annoyance to those of us who do. -- Isaac Asimov
     
    John W. Krahn, Jan 14, 2009
    #8
  9. Rajpreet

    Hans Mulder Guest

    cartercc wrote:

    > printf OUTFILE "$row[0]\t$row[1]\t$row[2]\t$row[3]\t$row[4]\t%d\n",
    > $row[3] + $row[4];


    That will break horribly if @row contains a '%' character.

    Better use:

    print OUTFILE join("\t", @row[0..4], $row[3] + $row[4]), "\n";

    Or, if you must use printf to format the added column:

    printf OUTFILE "%s\t%d\n", join("\t", @row[0..4]), $row[3] + $row[4];

    Hope his helps,

    -- HansM
     
    Hans Mulder, Jan 14, 2009
    #9
    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. helpful sql
    Replies:
    0
    Views:
    849
    helpful sql
    May 19, 2005
  2. Replies:
    5
    Views:
    632
  3. Replies:
    4
    Views:
    963
  4. =?Utf-8?B?YmVub2l0?=

    Read CSV - string Columns - Int columns

    =?Utf-8?B?YmVub2l0?=, May 8, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    452
    =?Utf-8?B?YmVub2l0?=
    May 8, 2006
  5. Jacksm
    Replies:
    2
    Views:
    890
    Paul Chalekian
    Nov 21, 2006
Loading...

Share This Page