SunQuest
           UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old December 28th, 2004, 11:05 AM
dmuxaf dmuxaf is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Overseas - Azores
Posts: 7 dmuxaf User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 54 m 36 sec
Reputation Power: 0
Adding in Unix

I've been thinking and cannot figure out a way to add a column of numbers in a file when each number I want is in a seperate line. For example, let's say the information below is my file:

blah blah blah 456 324 554
blah blah blah 567 234 987
blah blah blah 456 324 534
blah blah blah 456 224 554
blah blah blah 456 344 554
blah blah blah 456 324 544
blah blah blah 456 325 554
blah blah blah 456 324 554

Ok, how would I add the fourth column in this file (first column of numbers)? I know how to sort from each column, and that's about all. So far, the man pages for sort, grep, wc, etc..., have not helped me. But, I don't know what other commands might help me better. Thanks for your help!

Reply With Quote
  #2  
Old December 28th, 2004, 11:34 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,083 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 19 h 44 m 45 sec
Reputation Power: 9
sort, grep, wc have nothing to do w/ your probl,
sed, ed, vi, awk, perl or c are the friends for this.
assumed the input is, like you say:
>>>>> blah blah blah 567 234 987
have a look what sed can:

sed 's/\(.*\) \(.*\) \(.*\) \(.*\) \(.*\) \(.*\) /\1 \2 \3 aaaa\4 \4bbbbb \5 \6/' input>output

try just for fun and read the manual pages
feel free to optimize the code
for filemanipulations not needing numerical computing or formating sed is the best.
AND the same syntax works in ed, vi, awk, perl got a modern one, but should also support that.
so learn once and reuse.

Last edited by guggach : December 28th, 2004 at 11:40 AM. Reason: typo

Reply With Quote
  #3  
Old December 28th, 2004, 09:01 PM
Perderabo Perderabo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 121 Perderabo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 54 sec
Reputation Power: 5
Try:
awk '{x+=$4} END{print x}' file

Reply With Quote
  #4  
Old December 29th, 2004, 04:13 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,083 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 19 h 44 m 45 sec
Reputation Power: 9
sorry, i did not understand: (numerical) add.
perderabo is OK.

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Adding in Unix


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway