Help with ask adding columns | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Help with ask adding columns

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-29-2012
Homa Homa is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 9 December 2013, 9:57 AM EST
Posts: 67
Thanks: 29
Thanked 1 Time in 1 Post
Help with ask adding columns

Hello,

I am using AWK in UBUNTU 12.04.

I have a dataset as follows:


Code:
1 2 12 1 4 1 4 1 7 9 4 6 
1 2 4 5 7 8 45 7 4 5 7 5

What I want to do is to add the values of some columns to each other and print it in the same file as the new column while omitting the previous two columns to have output as follows as an example:

1+4+4=9 2+1+6=9


So:

Code:
9 9 12 1 4 1 7 9 
53 14 4 5 7 8 4 5

thank you very much

Last edited by Homa; 11-29-2012 at 09:35 AM..
Sponsored Links
    #2  
Old 11-29-2012
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 15 September 2014, 8:06 AM EDT
Posts: 1,649
Thanks: 58
Thanked 478 Times in 474 Posts
Assuming your file structure as it is.

Try....


Code:
 awk '{ $1=$1+$7+$(NF-1);$2=$2+substr($8,1,1)+$NF;$7=$8="";gsub(" +"," ",$0);NF-=2;}1' file

The Following User Says Thank You to pamu For This Useful Post:
Homa (11-29-2012)
Sponsored Links
    #3  
Old 11-29-2012
Homa Homa is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 9 December 2013, 9:57 AM EST
Posts: 67
Thanks: 29
Thanked 1 Time in 1 Post
Thanks a lot. Could you please explain these two parts of the code:


Code:
substr($8,1,1)
gsub(" +"," ",$0)
NF-=2;

    #4  
Old 11-29-2012
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 15 September 2014, 8:06 AM EDT
Posts: 1,649
Thanks: 58
Thanked 478 Times in 474 Posts
Quote:
Originally Posted by Homa View Post
Thanks a lot. Could you please explain these two parts of the code:


Code:
substr($8,1,1)
gsub(" +"," ",$0)
NF-=2;

I think you have edited your previous input....

As per your previous input i have considered as you want only first digit of $8 .

That's why i used substr()

substr($8,1,1) - Prints only first digit of $8

gsub(" +"," ",$0) - Replaces multiple spaces with single space. To get the FS as single space.

NF-=2 - Here we need to remove last two numbers. That's why we have edited NF to NF-2 . So NF is reduced by 2.

As per your current input you can remove that substr()

use


Code:
awk '{ $1=$1+$7+$(NF-1);$2=$2+$8+$NF;$7=$8="";gsub(" +"," ",$0);NF-=2;}1' file

The Following User Says Thank You to pamu For This Useful Post:
Homa (11-29-2012)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Adding columns of time LinuxRacr Shell Programming and Scripting 9 11-07-2012 03:45 PM
Adding loads of columns zajtat UNIX for Dummies Questions & Answers 3 02-23-2010 11:00 PM
Adding columns of two files chandra321 Shell Programming and Scripting 7 05-06-2009 12:11 PM
adding columns Kelam_Magnus Shell Programming and Scripting 12 02-10-2009 09:56 AM
Adding columns in csv chachabronson UNIX for Advanced & Expert Users 7 06-27-2008 09:06 AM



All times are GMT -4. The time now is 10:10 PM.