Help!! Need script to read files and add values by lines...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help!! Need script to read files and add values by lines...
# 1  
Old 09-14-2007
Error 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  
Old 09-14-2007
Code:
nawk '
{
   arr[FNR]+=$1
   fnr=FNR
}
END {
   for(i=1; i<=fnr; i++)
      print arr[i]
}
' *.dhm > result.txt

# 3  
Old 09-15-2007
Quote:
Originally Posted by dhuertas
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  
Old 09-16-2007
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add the values of the lines according to a condition

hi everybody :) I am a beginner in bash and I want to convert the result I have here I want it to be grouped by IP address so iwanna get for each ip adsress the addition of all bandwidth where ip is 100.1.1.15 in other words for 100.1.1.15 it groups me all the values whose ip address is... (11 Replies)
Discussion started by: aynar
11 Replies

2. Shell Programming and Scripting

Read file lines and pass line values as arguments.

Dears, Need help to implement below requirement A file (detail.txt)contain : 1st column: Stream 2nd column: PathAddress 3rd column: Counterlimit 4th column: TransactionDateColumn 5th column: DateType 6th column: SleepValue 7th column: Status Need to write a... (1 Reply)
Discussion started by: sadique.manzar
1 Replies

3. UNIX for Beginners Questions & Answers

awk GSUB read field values from multiple text files

My program run without error. The problem I am having. The program isn't outputting field values with the column headers to file.txt. Each of the column headers in file.txt has no data. MEMSIZE SECOND SASFoundation Filename The output results in file.txt should show: ... (1 Reply)
Discussion started by: dellanicholson
1 Replies

4. Shell Programming and Scripting

Read a file and replace values in a script

Hi , I have a property file placed in folder /usr/opt/temp/aorc.prop which has values given below . I need to read this file content and replace the node with actual values in a shell script . Each time the script shall replace the node value from the poperty file and execute a cfsend command and... (10 Replies)
Discussion started by: samrat dutta
10 Replies

5. Shell Programming and Scripting

Shell script to read little complex values

Dear All, I have attached a file. In that I want to read some of the values like 1. ExecutionTime 2. ClockTime etc. I want to read at a specified time. How can I do that? Thanks & Regards, linuxUser_ (9 Replies)
Discussion started by: linuxUser_
9 Replies

6. Shell Programming and Scripting

Query the table and return values to shell script and search result values from another files.

Hi, I need a shell script, which would search the result values from another files. 1)execute " select column1 from table_name" query on the table. 2)Based on the result, need to be grep from .wft files. could please explain about this.Below is the way i am using. #!/bin/sh... (4 Replies)
Discussion started by: Rami Reddy
4 Replies

7. Shell Programming and Scripting

Help with script to add two zeros to certain lines in a set of files?

Hello... I should be better with scripting but I am not so turning here for some help. I did search quite a bit using google and didn't find anything meeting my needs for this. What am after here is a script (in a redhat linux env) that will read in a small series of files (netbackup vault... (6 Replies)
Discussion started by: abg1969
6 Replies

8. Shell Programming and Scripting

Help with script to read lines from file and count values

Hi, I need some help with a script I'm trying to write. I have a log file containing references to a number of different webservices. I wish to write a script that will list the webservices with a count as to how many times they appear in the log. An example of the log file content: ... (2 Replies)
Discussion started by: gman2010
2 Replies

9. Shell Programming and Scripting

How can I add 4 lines together and then read the next 4 lines?

Hi, I have grep:ed on a file with which gives the below output. Total = 43 Total = 21 Total = 23 Total = 2 Total = 3 Total = 1 Total = 0 I would like to add the first 4th lines to a total amount, and do so on for the rest of the lines. I was thinking in terms of using while read.... (32 Replies)
Discussion started by: mr_andrew
32 Replies

10. Shell Programming and Scripting

awk/sed script to read values from parameter files

Hi, I am writing a shell program that executes a lot of Oracle SQL Files on different databases based on the enviroment setting value. I am trying to design a parameter file where i can store the environment values for all the databases in the below format Environment File File Name... (6 Replies)
Discussion started by: rajan_san
6 Replies
Login or Register to Ask a Question