Calculate the total


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calculate the total
# 8  
Old 03-02-2014
Here I think a complete re-write is best:

Code:
function print_row {
    printf "%8s |" "${Table[@]}"
    printf "\n"
    printf "=====%.0s====|" "${Table[@]}"
    printf "\n"
}

# Build header row
Table=("Month" )
for File in *.log;do
     Table+=( "$(date "+%b %Y" --date="${File:4:6}01")" )
done
Table+=( "Total" )
print_row

while read STR NAME; do
   Table=( "$NAME" $(awk -v S="$STR" -v N="$NAME" '
       FNR==1{f++}
       $1==S && $2==N{T++;C[f]++}
       END{
          for(i=1;i<=f;i++) printf " %d",C[i]
          printf " %d\n", T}' *.log ) )
    print_row
done < file.txt


Last edited by Chubler_XL; 03-02-2014 at 03:53 PM..
# 9  
Old 03-03-2014
thanks Chubler XL 's great job .

May be my question is still not clearly ,

what I would like to count is the number of the currence of the first string in file.txt , the file.txt is as below.
Code:
aaa 111
bbb 222

therefore , I would like to count the number of the string "aaa" and "bbb" in *.log , the "111" and "222" will be shown on the report only.

in aaaa201401.log , there are two "aaa" , two "bbb" ;
in aaaa201402.log , there are four "aaa" , five "bbb

the desired report should be as bwlow.

Code:
=========|=========|=========|=========|
   Month |Jan 2014 |Feb 2014 |   Total |
=========|=========|=========|=========|
     111 |       2 |       4 |       6 |
=========|=========|=========|=========|
     222 |       2 |       5 |       7 |
=========|=========|=========|=========|

There is a bit should be modify , would you please help ? thanks

Last edited by ust3; 03-03-2014 at 02:35 AM..
# 10  
Old 03-03-2014
That output is what is produced by the code above, using your input files from post #1.

If this is wrong, please build new input files show the problem.
# 11  
Old 03-03-2014
I have re-buile a new log files , it should be more clear .
What I hope to do is to check how many the string ( "aaa" and "bbb" ) are occurence in each file .

#vi file.txt

Code:
aaa this is description file 1 
bbb this is description file 2

#vi aaaa201401.log
Code:
aaa dfasf ffd
aaa dfags gf
bbb xdfas gfs 
bbb dfasf gfsd
bbb dfasgf

#vi aaaa201402.log
Code:
aaa fsdafhgh
aaa  fdasf hgh
aaa  fdasfas hgdh
aaa fsdafasf 45252
bbb fdsafa trt
bbb dfasfaf 543
bbb ghsghsgf gfs
bbb gfsg gsg
bbb gdsf gfsg

in aaaa201401.log , there are two "aaa" , three "bbb" ;
in aaaa201402.log , there are four "aaa" , five "bbb

the desired report should be as below.
Code:
===============================|=========|=========|=========|
    Month                      |Jan 2014 |Feb 2014 |  Total  |
===============================|=========|=========|=========|
    this is description file 1 |    2    |    4    |    6    |
===============================|=========|=========|=========|
    this is description file 2 |    3    |    5    |    8    |
===============================|=========|=========|=========|

# 12  
Old 03-03-2014
If you have groff on your system this might be an easier way to print the ascii table:

Code:
(
printf ".TS\nallbox;\nc c"
printf "%.0s c" *.log
printf "\nl r"
printf "%.0s r" *.log
printf ".\n Month"
for File in *.log;do
     printf "\t%s" "$(date "+%b %Y" --date="${File:4:6}01")"
done
printf "\tTotal\n"

while read STR NAME; do
   printf "%s" "$NAME"
   awk -v S="$STR" '
       FNR==1{f++}
       $1==S{T++;C[f]++}
       END{
          for(i=1;i<=f;i++) printf "\t%d",C[i]
          printf "\t%d\n", T}' *.log
done < file.txt
echo ".TE" ) | tbl | groff -T ascii | grep -v '^$'

Output:
Code:
+---------------------------+----------+----------+-------+
|           Month           | Jan 2014 | Feb 2014 | Total |
+---------------------------+----------+----------+-------+
|this is description file 1 |        2 |        4 |     6 |
+---------------------------+----------+----------+-------+
|this is description file 2 |        3 |        5 |     8 |
+---------------------------+----------+----------+-------+

---------- Post updated at 04:42 AM ---------- Previous update was at 03:29 AM ----------

If your using an xterm (or such) you could try groff -T utf8 instead of groff -T ascii:

Code:
┌───────────────────────────┬──────────┬──────────┬───────┐
│           Month           │ Jan 2014 │ Feb 2014 │ Total │ 
├───────────────────────────┼──────────┼──────────┼───────┤ 
│this is description file 1 │        2 │        4 │     6 │ 
├───────────────────────────┼──────────┼──────────┼───────┤ 
│this is description file 2 │        3 │        5 │     8 │ 
└───────────────────────────┴──────────┴──────────┴───────┘


Last edited by Chubler_XL; 03-03-2014 at 01:36 PM..
# 13  
Old 03-04-2014
Quote:
Originally Posted by Chubler_XL
If you have groff on your system this might be an easier way to print the ascii table:

Code:
(
printf ".TS\nallbox;\nc c"
printf "%.0s c" *.log
printf "\nl r"
printf "%.0s r" *.log
printf ".\n Month"
for File in *.log;do
     printf "\t%s" "$(date "+%b %Y" --date="${File:4:6}01")"
done
printf "\tTotal\n"

while read STR NAME; do
   printf "%s" "$NAME"
   awk -v S="$STR" '
       FNR==1{f++}
       $1==S{T++;C[f]++}
       END{
          for(i=1;i<=f;i++) printf "\t%d",C[i]
          printf "\t%d\n", T}' *.log
done < file.txt
echo ".TE" ) | tbl | groff -T ascii | grep -v '^$'

Output:
Code:
+---------------------------+----------+----------+-------+
|           Month           | Jan 2014 | Feb 2014 | Total |
+---------------------------+----------+----------+-------+
|this is description file 1 |        2 |        4 |     6 |
+---------------------------+----------+----------+-------+
|this is description file 2 |        3 |        5 |     8 |
+---------------------------+----------+----------+-------+

---------- Post updated at 04:42 AM ---------- Previous update was at 03:29 AM ----------

If your using an xterm (or such) you could try groff -T utf8 instead of groff -T ascii:

Code:
┌───────────────────────────┬──────────┬──────────┬───────┐
│           Month           │ Jan 2014 │ Feb 2014 │ Total │ 
├───────────────────────────┼──────────┼──────────┼───────┤ 
│this is description file 1 │        2 │        4 │     6 │ 
├───────────────────────────┼──────────┼──────────┼───────┤ 
│this is description file 2 │        3 │        5 │     8 │ 
└───────────────────────────┴──────────┴──────────┴───────┘

Hi Chubler_XL ,

Very thanks for your work , I hope this is my last question .
If I would like to check the occurrence of the whole string "aaa" , the string "aaa" may not be begins and ended with space , for example , if the log is as below , it still count the occurrence of "aaa" is 2 , how to modify the script ? thanks

Code:
XXaaaYY dfasf ffd
CCaaaDD dfags gf
bbb xdfas gfs 
bbb dfasf gfsd
bbb dfasgf


Last edited by ust3; 03-04-2014 at 10:22 PM..
# 14  
Old 03-04-2014
replace $1==S{T++;C[f]++} with $1~S{T++;C[f]++}
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calculate total memory using free -m

Hi I am trying to calculate memory used by Linux System free -m total used free shared buffers cached Mem: 32109 31010 1099 0 3600 7287 -/+ buffers/cache: 20121 11987 Swap: 10239 1282 8957 Now according to my requirement Im calculating memory using below cmd free -m | awk 'NR==3{printf... (2 Replies)
Discussion started by: sam@sam
2 Replies

2. Shell Programming and Scripting

Calculate the total 4 field based on the conditions

Please help me to write a script Match with ACNO & NAME if it matched calculate the total val1 val2 val3 and val4 and GT is total of ACNO wise.please check the output Table ----------------- 1005|ANDP|ACN|20|50|10|30 1005|ANDP|ACN|20|10|30|40 1001|AND|NAC|40|50|40|50... (22 Replies)
Discussion started by: kalia4u
22 Replies

3. Shell Programming and Scripting

Calculate total value from a row

HI I have a file # cat marks.txt MARKS LIST 2013 Name english french chinese latin total_marks wer 34 45 67 23 wqa 12 39 10 56 wsy 23 90 23 78 Now i need to find the total marks of each student using... (11 Replies)
Discussion started by: Priya Amaresh
11 Replies

4. Shell Programming and Scripting

Calculate total of log by hour

Hi, Just wondering, is there anyway I can get the total of logs generated by hours ? Let say I have these logs, Sep 23 04:48:43 hsbcufs: NOTICE: realloccg /: file system full Sep 23 04:48:47 hsbcufs: NOTICE: alloc: /: file system full Sep 23 04:48:51 hsbcufs: NOTICE: realloccg /: file... (14 Replies)
Discussion started by: dehetoxic
14 Replies

5. Shell Programming and Scripting

Calculate total space, total used space and total free space in filesystem names matching keyword

Good afternoon! Im new at scripting and Im trying to write a script to calculate total space, total used space and total free space in filesystem names matching a keyword (in this one we will use keyword virginia). Please dont be mean or harsh, like I said Im new and trying my best. Scripting... (4 Replies)
Discussion started by: bigben1220
4 Replies

6. Shell Programming and Scripting

Calculate total sum from a file

The file content is dynamic and using this format: name1 number1 name2 number2 name3 number3 name4 number4 .................... Need a smooth way to calculate the sum of all the numbers in that file (number1 + number2 + number3 + number4........ = total ) (11 Replies)
Discussion started by: TehOne
11 Replies

7. Shell Programming and Scripting

awk script to calculate total

Hi First field is the Record Type. A Record Type 5 can have multiple Record Type 6's before another Record Type 5 appears. I want to calculate the total of fields at position 8-11 on Record type 6 when Record Type 5 has a field at position 11-14 equals to '2222'. then it should delete the lines... (2 Replies)
Discussion started by: appsguy616
2 Replies

8. Shell Programming and Scripting

Awk help needed to calculate total

Hi all, I have a flat file like 10 steven 25 mike 47 Charles 127 Nancy 34 steven 23 mike 67 Charles 7761 Nancy 8 steven 54 mike 88 Charles 1267 Nancy I need to calculate the total of steven and all the members , for this I am using like grep "`sed -n 1p patterns.txt`"... (7 Replies)
Discussion started by: senthilkumar_ak
7 Replies
Login or Register to Ask a Question