Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Add columns of a File

Reply
Thread Tools

Add columns of a File

 
 
Rajpreet
Guest
Posts: n/a
 
      01-14-2009
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

 
Reply With Quote
 
 
 
 
Tad J McClellan
Guest
Posts: n/a
 
      01-14-2009
Rajpreet <(E-Mail Removed)> 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/"
 
Reply With Quote
 
 
 
 
Rajpreet
Guest
Posts: n/a
 
      01-14-2009
On Jan 14, 6:21*pm, Tad J McClellan <(E-Mail Removed)> wrote:
> Rajpreet <(E-Mail Removed)> 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
 
Reply With Quote
 
test
Guest
Posts: n/a
 
      01-14-2009
On Jan 14, 7:08*pm, Rajpreet <(E-Mail Removed)> wrote:
> On Jan 14, 6:21*pm, Tad J McClellan <(E-Mail Removed)> wrote:
>
>
>
>
>
> > Rajpreet <(E-Mail Removed)> 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?
 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      01-14-2009
Rajpreet <(E-Mail Removed)> 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/"
 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      01-14-2009
test <(E-Mail Removed)> wrote:
> On Jan 14, 7:08*pm, Rajpreet <(E-Mail Removed)> wrote:
>> On Jan 14, 6:21*pm, Tad J McClellan <(E-Mail Removed)> 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/"
 
Reply With Quote
 
cartercc
Guest
Posts: n/a
 
      01-14-2009
On Jan 14, 7:23*am, Rajpreet <(E-Mail Removed)> 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
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      01-14-2009
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
 
Reply With Quote
 
Hans Mulder
Guest
Posts: n/a
 
      01-14-2009
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Align table columns with gridview columns Jacksm ASP .Net 2 11-21-2006 09:49 PM
Read CSV - string Columns - Int columns =?Utf-8?B?YmVub2l0?= ASP .Net 0 05-08-2006 02:11 PM
database columns vs. gui columns and sorting ittay.dror@gmail.com Java 5 03-04-2006 10:48 AM
CSS columns problem - faux columns don't work henrybranson@hotmail.com HTML 4 11-24-2005 10:05 AM
convert rows to columns and columns to rows helpful sql ASP .Net 0 05-19-2005 06:03 PM



Advertisments