|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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! |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
Try:
awk '{x+=$4} END{print x}' file |
|
#4
|
|||
|
|||
|
sorry, i did not understand: (numerical) add.
perderabo is OK. |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Adding in Unix |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|