Add columns of a File

R

Rajpreet

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
 
T

Tad J McClellan

Rajpreet said:
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
 
R

Rajpreet

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




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


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


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

Thanks Tad. I will try that and will come back in case of issues.
Thanks again
 
T

test

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?
 
T

Tad J McClellan

test said:
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;
}
 
C

cartercc

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
 
J

John W. Krahn

Rajpreet said:
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
 
H

Hans Mulder

cartercc said:
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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top