The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Script lines of a Log Files Nysif Steve Shell Programming and Scripting 2 08-30-2007 07:55 AM
Shell Script to read specific lines in a file varshanswamy Shell Programming and Scripting 5 08-22-2005 04:12 PM
Korn Shell Script - Read File & Search On Values run_unx_novice Shell Programming and Scripting 2 06-15-2005 04:20 AM
help with cshell script to read 1 or more lex files homerj546 Shell Programming and Scripting 1 04-18-2004 07:01 AM
Script with read/write Files steiner Shell Programming and Scripting 5 07-25-2003 07:46 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-14-2007
Registered User
 

Join Date: Sep 2007
Posts: 1
Exclamation Help!! Need script to read files and add values by lines...

Hi All, I really need your help. I am a begginner in shell script and I believe this is a very simple issue.

I have in my directory, n-files, like 1.dhm, 2.dhm, 3.dhm.
These files have 1 column with 1 value per line, like:

1.dhm
------
10
20
30
40
50


2.dhm
------

30
50
20
50
80


What I need here, is to make a result file, adding each per file, in this case:

result.txt
---------
40
70
50
90
130

Can someone help me?

Thanks!!
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 09-14-2007
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 2,999
Code:
nawk '
{
   arr[FNR]+=$1
   fnr=FNR
}
END {
   for(i=1; i<=fnr; i++)
      print arr[i]
}
' *.dhm > result.txt
Reply With Quote
  #3 (permalink)  
Old 09-15-2007
cfajohnson's Avatar
Registered User
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 487
Quote:
Originally Posted by dhuertas View Post
I have in my directory, n-files, like 1.dhm, 2.dhm, 3.dhm.
These files have 1 column with 1 value per line, like:

1.dhm
------
10
20
30
40
50


2.dhm
------

30
50
20
50
80


What I need here, is to make a result file, adding each per file, in this case:

result.txt
---------
40
70
50
90
130

Code:
paste -d+ *.dhm | bc > result.txt
Reply With Quote
  #4 (permalink)  
Old 09-15-2007
Registered User
 

Join Date: Sep 2007
Posts: 4
Good and cute solution. But I think we have to verify that each file has same number of records (lines) and all lines are numbers only.

Soham
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 08:41 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0