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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Output no. of rows and total value of a file to a new file
# 1  
Old 03-22-2010
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:

Code:
No. of rows : 15
Total value : 16208.49

File: ajtest.dat:

Code:
3033*94917,PL123000100049,+000097.37,20-Mar-2008,24-Aug-2008
3033*96345,PL091708600108,+001724.64,17-Sep-2008,02-Aug-2009
3033*96479,PL054808900070,+000201.18,22-Oct-2008,24-Feb-2009
3033*98774,PL056901201003,+001313.52,31-Mar-2009,07-Jun-2009
3033*96420,PL090007900061,+003802.54,26-Aug-2008,29-Jun-2009
3033*94089,PL090402200131,+005008.13,26-Feb-2008,25-Mar-2009
3033*100366,PL090708300112,+000141.94,21-Sep-2009,08-Nov-2009
3033*97563,PL090714000249,+000000.84,12-Jan-2009,25-Jan-2009
3033*94845,PL090814400090,+000081.12,20-Feb-2008,31-Jul-2008
3033*90236,PL090912800080,+000116.69,29-Sep-2008,18-Jan-2009
3033*95124,PL015104300203,+000260.41,14-Apr-2008,01-Dec-2008
3033*95474,PL015301000256,+000048.96,04-Jun-2008,30-Oct-2008
3033*78033,PL015304400202,+000181.44,18-Aug-2008,12-Feb-2009
3033*95068,PL015308200270,+001801.71,02-May-2008,24-Aug-2009
3033*98498,PL052402800063,+001428.00,26-Mar-2009,25-Nov-2009


Last edited by radoulov; 03-23-2010 at 07:42 AM.. Reason: Please use code tags!
# 2  
Old 03-22-2010
Try this:

Code:
awk -F ',' '{ sum = sum + $3} { sum1 = sum1 + 1} END { print "No. of rows : "sum1" Total value : "sum}' ajtest.dat >> aj1



---------- Post updated at 11:58 AM ---------- Previous update was at 11:53 AM ----------

Try this (print next line):

Code:
awk -F ',' '{ sum = sum + $3} { sum1 = sum1 + 1} END { print "No. of rows : "sum} NR  { print "Total value : "sum1}' ajtest.dat | tail -2 > aj1



---------- Post updated at 12:02 PM ---------- Previous update was at 11:58 AM ----------

Correct one is :

Code:
awk -F ',' '{ sum = sum + $3} { sum1 = sum1 + 1} END { print "Total Value : "sum} NR  { print "No. of rows: : "sum1}' ajtest.dat   | tail -2 > aj1


Last edited by radoulov; 03-23-2010 at 07:42 AM.. Reason: Please use code tags!
# 3  
Old 03-22-2010
Hello Aj,

You can use my below code to achive this. This script accepts file name from command line.
Code:
#!/bin/ksh

file_name=$1 ;
no_rows=0 ;
total=0 ;

while read line
do
temp_total=`echo $line | cut -d "," -f3 | tr -d '+,-' ''` ;
no_rows=`expr ${no_rows} + 1` ;
total=`echo ${temp_total}+${total} | bc` ;
done < $file_name

echo "Number of rows = $no_rows" ;
echo "Third column total = $total" ;

-Nithin.
# 4  
Old 03-23-2010
Hi Jairaj,
Thank you for your reply.

I have tried the script entitled 'Correct one' but when I execute it, I get the message:
awk: syntax error near line 1
awk: bailing out near line 1

What have I done wrong?
# 5  
Old 03-23-2010
Code:
nawk -F, '{sum+=$3}END{print "No. of rows: " NR"\nTotal value: " sum}' infile

# 6  
Old 03-23-2010
Hi Malcomex999,

Thank you for your reply.

I have run your script and it works except for it rounds the pence up to one decimal place. How do I get it to display to two decimal places?
# 7  
Old 03-23-2010
All are in the single line.

Code:
awk -F ',' '{ sum = sum + $3} { sum1 = sum1 + 1} END { print "Total Value : "sum} NR { print "No. of rows: : "sum1}' ajtest.dat | tail -2 > aj1


Last edited by radoulov; 03-23-2010 at 07:43 AM.. Reason: Code tags, please!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script (sh file) logic to compare contents of one file with another file and output to file

Shell script logic Hi I have 2 input files like with file 1 content as (file1) "BRGTEST-242" a.txt "BRGTEST-240" a.txt "BRGTEST-219" e.txt File 2 contents as fle(2) "BRGTEST-244" a.txt "BRGTEST-244" b.txt "BRGTEST-231" c.txt "BRGTEST-231" d.txt "BRGTEST-221" e.txt I want to get... (22 Replies)
Discussion started by: pottic
22 Replies

2. Shell Programming and Scripting

Moving or copying first rows and last rows into another file

Hi I would like to move the first 1000 rows of my file into an output file and then move the last 1000 rows into another output file. Any help would be great Thanks (6 Replies)
Discussion started by: kylle345
6 Replies

3. UNIX for Dummies Questions & Answers

merging rows into new file based on rows and first column

I have 2 files, file01= 7 columns, row unknown (but few) file02= 7 columns, row unknown (but many) now I want to create an output with the first field that is shared in both of them and then subtract the results from the rest of the fields and print there e.g. file 01 James|0|50|25|10|50|30... (1 Reply)
Discussion started by: A-V
1 Replies

4. UNIX for Dummies Questions & Answers

Write the total number of rows in multiple files into another file

Hello Friends, I know you all are busy and inteligent too... I am stuck with one small issue if you can help me then it will be really great. My problem is I am having some files i.e. Input.txt1 Input.txt2 Input.txt3 Now my task is I need to check the total number of rows in... (4 Replies)
Discussion started by: malaya kumar
4 Replies

5. Shell Programming and Scripting

Extract rows from file based on row numbers stored in another file

Hi All, I have a file which is like this: rows.dat 1 2 3 4 5 6 3 4 5 6 7 8 7 8 9 0 4 3 2 3 4 5 6 7 1 2 3 4 5 6 I have another file with numbers like these (numbers.txt): 1 3 4 5 I want to read numbers.txt file line by line. The extract the row from rows.dat based on the... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

6. UNIX for Dummies Questions & Answers

Read rows from source file and concatenate output

Hi guys; TBH I am an absolute novice, when it comes to scripting; I do have an idea of the basic commands... Here is my problem; I have a flatfile 'A' containing a single column with multiple rows. I have to create a script which will use 'A' as input and then output a string in in the... (0 Replies)
Discussion started by: carlos_anubis
0 Replies

7. Shell Programming and Scripting

Unix Script file to Append Characters before rows in file.

Hi Experts, I am working on HP-UX. I am new to shell scripting. I would like to have a shell script which will prefix: 1. "H|" before first row of my file and, 2. "T" for all other rows of the file. For Example - File before running the script 20100430|4123451810|218.50|TC 20100430 ... (4 Replies)
Discussion started by: phani333
4 Replies

8. Shell Programming and Scripting

Dynamic output file generation using a input text file with predefined output format

Hi, I have two files , one file with data file with attributes that need to be sent to another file to generate a predefined format. Example: File.txt AP|{SSHA}VEEg42CNCghUnGhCVg== APVG3|{SSHA}XK|"password" AP3|{SSHA}XK|"This is test" .... etc --------- test.sh has... (1 Reply)
Discussion started by: hudson03051nh
1 Replies

9. Shell Programming and Scripting

delete rows in a file based on the rows of another file

I need to delete rows based on the number of lines in a different file, I have a piece of code with me working but when I merge with my C application, it doesnt work. sed '1,'\"`wc -l < /tmp/fileyyyy`\"'d' /tmp/fileA > /tmp/filexxxx Can anyone give me an alternate solution for the above (2 Replies)
Discussion started by: Muthuraj K
2 Replies

10. Shell Programming and Scripting

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 ... (7 Replies)
Discussion started by: Pablo_beezo
7 Replies
Login or Register to Ask a Question