The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Changing one column of delimited file column to fixed width column manneni prakash Shell Programming and Scripting 5 06-22-2009 06:27 AM
how to read the column and print the values under that column gemini106 Shell Programming and Scripting 6 03-28-2008 07:05 AM
How to check Null values in a file column by column if columns are Not NULLs Mandab Shell Programming and Scripting 7 03-15-2008 09:57 AM
replace a column values with the first value in column sumeet UNIX for Advanced & Expert Users 3 02-06-2007 01:13 PM
Replace 10th column with a new column--- Terriblly hurry ahmedwaseem2000 Shell Programming and Scripting 2 09-06-2005 02:10 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-25-2008
baconbasher baconbasher is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 5
Column containing sum using awk

Hi All,

I am trying to add a column that contains the sum of the previous column repeated. IE

1
2
3
4

I would like to get:

1 10
2 10
3 10
4 10

Advice? I can get
1 1
2 3
3 6
4 10

but the former is really what I want.

Thanks
  #2 (permalink)  
Old 07-25-2008
krishmaths krishmaths is offline
Registered User
  
 

Join Date: Sep 2006
Location: Mysore, India
Posts: 191
If you are okay to use two commands, you may try this


Code:
export s=`awk '{sum+=$1} END{print sum}' tempfile`; awk -v var=$s '{print $1" "var}' tempfile

  #3 (permalink)  
Old 07-25-2008
baconbasher baconbasher is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 5
Yeah...I thought about doing it that way, I had just hoped there was cleaner way of doing it.

Thanks.
  #4 (permalink)  
Old 07-25-2008
radoulov's Avatar
radoulov radoulov is online now Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,915
Use nawk or /usr/xpg4/bin/awk on Solaris.


Code:
awk 'NR==FNR{_+=$1;next}$0=$0FS _' file file

  #5 (permalink)  
Old 07-25-2008
baconbasher baconbasher is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 5
Thanks.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On




All times are GMT -4. The time now is 11:06 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0