The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 10:55 AM
Shell Script to read specific lines in a file varshanswamy Shell Programming and Scripting 5 08-22-2005 07:12 PM
Korn Shell Script - Read File & Search On Values run_unx_novice Shell Programming and Scripting 2 06-15-2005 07:20 AM
help with cshell script to read 1 or more lex files homerj546 Shell Programming and Scripting 1 04-18-2004 10:01 AM
Script with read/write Files steiner Shell Programming and Scripting 5 07-25-2003 10:46 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-14-2007
dhuertas dhuertas is offline
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!!
  #2 (permalink)  
Old 09-14-2007
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Code:
nawk '
{
   arr[FNR]+=$1
   fnr=FNR
}
END {
   for(i=1; i<=fnr; i++)
      print arr[i]
}
' *.dhm > result.txt
  #3 (permalink)  
Old 09-15-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
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
  #4 (permalink)  
Old 09-15-2007
Soham Soham is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 7
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
Sponsored Links
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 02:03 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