I have a while loop which looks for files and then sets a variable to give me the record count of each file:
current_members=`wc -l ${DATA_DIR}/$MEMBERS_FILENAME | nawk '{ printf "%d\n", $0}'`
I am out putting the totals into a file:
echo $current_members >> ../data/out_total_members.dat
out_total_members.dat could contain:
4000
1000
2000
What I want to do is set another variable that gives the total count of the above file i.e. 7000
How can this be done??
#!/bin/ksh
total=$(echo "$(tr '\n' '+' < ../data/out_total_members.dat;)0" |bc)
You don't need awk if you redirect the input to wc:
current_members=$( wc -l < ${DATA_DIR}/$MEMBERS_FILENAME )
var=$(( $( tr '\n' + < out_total_members.dat ) ))
var=$( awk '{t+= $1} END { print t }' out_total_members.dat )
Thanks for the help, it now works great
F.A.O vgersh99,
can you explain what this is actually doing:
I understand that it reads in the file to the command not sure what tr means?
$(echo "$(tr '\n' '+' < ../data/out_previous_total_members.dat; )0" |bc)
also can this be used to times, divide and minus the contents of the file.
Take a look at this thread.
Yes, similar implementation can be used for other math ops.
I take it that you simply cannot change your code to do other math ops i.e. instead of '+' can I have '-', '/','*' ?????
you take it wrong - reread the explanation - '/':
$(echo "scale=5; $(tr '\n' '/' < ../data/out_previous_total_members.dat; )1" |bc)