total output from a file created in a while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting total output from a file created in a while loop
# 1  
Old 01-28-2009
total output from a file created in a while loop

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??
# 2  
Old 01-28-2009
Code:
#!/bin/ksh

total=$(echo "$(tr '\n' '+' < ../data/out_total_members.dat;)0" |bc)


Last edited by vgersh99; 01-28-2009 at 01:08 PM.. Reason: ooops '0', not '1'
# 3  
Old 01-28-2009
Quote:
Originally Posted by Pablo_beezo
I have a while loop which looks for files and then sets a variable to give me the record count of each file:

Code:
current_members=`wc -l ${DATA_DIR}/$MEMBERS_FILENAME | nawk '{ printf "%d\n", $0}'`


You don't need awk if you redirect the input to wc:

Code:
current_members=$( wc -l < ${DATA_DIR}/$MEMBERS_FILENAME )

Quote:
I am out putting the totals into a file:

Code:
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

Code:
var=$(( $( tr '\n' + < out_total_members.dat ) ))

Code:
var=$( awk '{t+= $1} END { print t }' out_total_members.dat )


Last edited by cfajohnson; 01-28-2009 at 01:12 PM..
# 4  
Old 01-29-2009
Thanks for the help, it now works great
# 5  
Old 01-29-2009
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.
# 6  
Old 01-29-2009
Quote:
Originally Posted by Pablo_beezo
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.
# 7  
Old 01-29-2009
I take it that you simply cannot change your code to do other math ops i.e. instead of '+' can I have '-', '/','*' ?????
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Echo For ... Loop output into a file

Hi, All: I have one for ... loop code to generate a list of rename statement. However, echo the loop output on screen is OK. But store the echo output into a file is not working. So come here to seek help. My basic code is: ! /bin/ksh echo > DAS_VetFed.txt dir=/mydirectory cd $dir files=`ls... (6 Replies)
Discussion started by: duke0001
6 Replies

2. UNIX for Beginners Questions & Answers

For loop output to excel file

Hi, I have a shell script which analyses the log folder for a specific string and throws me the output. I have used for loop since it does this in multiple servers. Now I want to save the output in a excel in the below format. Can someone please help? The output which I get Server1 : count... (1 Reply)
Discussion started by: srilaxman
1 Replies

3. UNIX for Advanced & Expert Users

'total 0' in ls output

I have a Linux box, where some NAS shares are mounted. One script is running on it and failing because it first looks at second line of 'ls -l', when it says 'total 0', it fails. If that directory is on local disk, it says 'total 38' and when same data is on NAS share, it says 'total 0' and it... (2 Replies)
Discussion started by: solaris_1977
2 Replies

4. Homework & Coursework Questions

Loop to Convert a list from an input file and output it to another file

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: A) Write a script, which will take input from a file and convert the number from Centigrade to Fahrenheit... (5 Replies)
Discussion started by: AliTheSnake
5 Replies

5. Shell Programming and Scripting

awk: too many output files created from while loop

I am using awk to read lines from a CSV file then put data into other files. These other files are named using the value of a certain column. Column 7 is a name such as "att" or "charter" . I want to end up with file names with the value of column 7 appended to them, like this: ... (5 Replies)
Discussion started by: dodgerfan78
5 Replies

6. Solaris

What does total mean in ls -l output?

# ls -l | more total 12861 drwxr-xr-x 3 root root 512 Jun 20 21:57 Desktop drwxr-xr-x 2 root root 512 Oct 16 2010 Documents lrwxrwxrwx 1 root root 9 Oct 16 2010 bin -> ./usr/bin drwxr-xr-x 7 root sys 512 Oct 16 2010 boot drwxr-xr-x... (2 Replies)
Discussion started by: chidori
2 Replies

7. Shell Programming and Scripting

Output no. of rows and total value of a file to a new file

I am relatively new to Unix and I would be grateful if someone could help me with the syntax of a script. I am trying to write a Unix script against the file ‘ajtest.dat’ (see below) so that it outputs the number of rows and total value of column 3 in a file called ‘aj1’ as follows: No. of... (11 Replies)
Discussion started by: ajmutley
11 Replies

8. Shell Programming and Scripting

isql output file not created while running it through cron

#!/bin/ksh file="/pkgs/roots/scripts/ISQL_op.txt" isql -H 11.11.11.111:1111 -U myUser -P myPwd -o $file << eof go select * from Table1 go eof my cron entry 00 08 03 11 * /pkgs/roots/scripts/testc.ksh file permission of the script is correct, i have used absolute path everywhere. ... (2 Replies)
Discussion started by: vikram3.r
2 Replies

9. Shell Programming and Scripting

Trouble with a file path as awk output in for loop

When I run the following command in the shell it works fine. It prints a city name and then a path for a file. ~$ for i in `awk -F':' '{print $0}' /home/knoppix/Desktop/data/subs | grep -m 1 $ city | sed "s/:/ /"` >do >echo $i >done Now, when I place it in this shell script (sh) it prints... (6 Replies)
Discussion started by: afroCluster
6 Replies

10. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies
Login or Register to Ask a Question