Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding lines from files 1

Status
Not open for further replies.

scooter6

IS-IT--Management
Jul 10, 2001
44
US
I need to write a script to add the one-line value of several files and put the total of the addition into another file.

For example, I have 10 files named errlog.box1
errlog.box2
errlog.box3 ...,etc

each file only contains a number...so, suppose if I do a more on errlog.box1 and it shows 532
more on errlog.box2 and it shows 147
more on errlog.box3 and it shows 312

I need a way to create a file called errlog.total that will
ONLY have the total of the other 3 files..in this case, it should only show one line that says 991

any help is greatly appreciated

thanks
 
Hi Scooter,
Here is example for two file.
Try this on command line it works for me.
set t = `cat test`
set t1 = `cat test1`
echo $t + $t1 | bc

Then
For multiple file you can do this
#!/bin/csh
set files = (file1 file2 file3)
set t = 0;
foreach file ($files)
set t1 = `cat $file`
set t = `echo $t+$t1 |bc`
end
echo $t
Sachin
 
When run from the command line using the first example you gave me, the result I get is

bc: Syntax error on line 1, teletype


I am operation on SCO OpenServer 5.0.5

There must be a way to use awk or something to get this to work...I just can't get it to...

thanks for your help anyways....any other ideas?
anyone? help :)

thanks
 
run this in the directory where the errlog.box(s) files are. you might need to adjust the wildcard for your needs.

#!/usr/bin/ksh
x=0
for i in `cat errlog*` ; do
let x=$x+$i
done
echo $x

crowe
 
thanks crowe...that helps a lot....

now, can I also set a "for" command inside of that "for" command??

For example, if at the end of the day I have in my directory
the following:

errlog.box1
errlog.box2
syslog.box1
syslog.box2
number.box1
number.box2

(keep in mind, all of these files have only a one-line entry of a whole number)can I do an "l" of the directory,
cat it to a text file, and then......

right after midnight, I want to run that script that will
take your script above and expand it a bit to end up with

errlog.total
syslog.total
number.total

(these files would represent the total of the ones mentioned above)

could I take your script and do this:

#!/usr/bin/ksh
x=0
for file in `cat logs.txt` ; do (logs.txt being my listing)
let x=$x+$i
echo $x

 
no problem, the only thing I can think of is you might want to use different variable names in the second for loop, but otherwise, it should work just the same.

crowe
 
thanks for all your help crowe...I got it working....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top