Calculate the total


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calculate the total
# 1  
Old 02-12-2014
Calculate the total

Hi All ,

I have the following script as below , I tried to modify to meet the requirement , could someone help ? very thanks

================================================================================================
Code:
while read STR NAME; do

Total=0
MyString="$STR"
GetData () {
AwkMyString="$( sed -e 's/[\/*?.+{}]/\\&/g' -e 's/[[]/\\&/g' -e 's/[]]/\\&/g' <<< "${MyString}" )"
for File in *.log;do
     Date="$(date "+%b %Y" --date="${File:4:6}01")"
     awk 'BEGIN{C=0}{for (i=1;i<=NF;i++) if ( $i ~ /'${AwkMyString}'/ ){C++}} END{printf "%s %s?","'"${Date}"'",C}' "$File"

done
}

 echo "No. of \"${MyString}\" in $(date "+%b %Y" --date="${File:4:6}01") = $Count"

   Total=$(( $Count + $Total ))
while read -d '?' Month Year Count;do
    TableDate+=("${Month} ${Year}")
    TableCount+=("${Count}")
done <<< $(GetData)

printf "%10s" "|"

for i in $(seq 0 $(( ${#TableDate[@]} - 1 )) );do
    printf "%s |" "${TableDate[$i]}"
done
printf "  Total  |\n"
for i in $(seq 0 $(( ${#TableCount[@]} +1 )) );do
    printf "=========|"
done
printf "\n"
printf "%10s" "|"
for i in $(seq 0 $(( ${#TableCount[@]} - 1 )) );do
    printf "%*s%*s" $(( $((11 + ${#TableCount[$i]}))/2 )) "${TableCount[$i]}" $(( $((10 - ${#TableCount[$i]}))/2 )) "|"
done
printf "\n"
echo "======="


done < file.txt

================================================================================================


the files are as below
=========================

#vi file.txt
Code:
aaa 111
bbb 222

#vi aaaa201401.log
Code:
aaa  111
aaa  111
bbb  222
bbb  222

#vi aaaa201402.log
Code:
aaa  111
aaa  111
aaa  111
aaa  111
bbb  222
bbb  222
bbb  222
bbb  222
bbb  222



the result when I run the above script is as below
==================================================

#my_script

Code:
No. of "aaa" in Feb 2014 = 
         |Jan 2014 |Feb 2014 |  Total  |
=========|=========|=========|=========|
         |     2   |     4   |
=======
No. of "bbb" in Feb 2014 = 
         |Jan 2014 |Feb 2014 |Jan 2014 |Feb 2014 |  Total  |
=========|=========|=========|=========|=========|=========|
         |     2   |     4   |     2   |     5   |
=======



What I would like to do is , list the number of occurance of for each month , then calculate the total amount of all months.


As you could see , the output data is dulicated and without Total amount , what I would like the output is as below , could advise how to change it ? very thanks .

#my_script

Code:
No. of "aaa" in Feb 2014 = 
|Jan 2014 |Feb 2014 |  Total  |
|=========|=========|=========|
|     2   |     4   |    6

No. of "bbb" in Feb 2014 = 
|Jan 2014 |Feb 2014 |  Total  |
|=========|=========|=========|
|     2   |     5   |    7

Moderator's Comments:
Mod Comment Please use CODE tags to mark sample code, sample input, and sample output rather than depending on moderators to edit your posts.

Last edited by Don Cragun; 02-12-2014 at 10:47 PM.. Reason: Add CODE tags.
# 2  
Old 02-13-2014
You can get the needed data in a lazy way (i have no doubt it may be improved) but you can go with something like :
Code:
grep -E "^" aaa*.log | sed 's/.log:/ /;s/^[^0-9]*//' | awk '{a[$2" "$1]++}END{for(i in a) print i, a[i]}' | sort -k 1

or
Code:
$ grep -E "^" aaa*.log | sed 's/\(..\).log:/ \1 /;s/^[^0-9]*//' | uniq -c | awk '{a[$4" "$2]+=$1;print}END{for(i in a)print "Tot",i,"=",a[i]}'
      2 2014 01 aaa  111
      2 2014 01 bbb  222
      4 2014 02 aaa  111
      5 2014 02 bbb  222
Tot aaa 2014 = 6
Tot bbb 2014 = 7

The formatting is then just a matter of cosmetic

Last edited by ctsgnb; 02-13-2014 at 06:13 PM..
# 3  
Old 02-13-2014
In your above script you could replace:

Code:
printf "\n"
echo "======="


done < file.txt

with:

Code:
LineTotal=$(($(IFS=+; echo "${TableCount[*]}")))
printf "%*s%*s\n" $(( $((11 + ${#LineTotal}))/2 )) "$LineTotal" $(( $((10 - ${#LineTotal}))/2 )) "|"

done < file.txt

# 4  
Old 02-13-2014
Quote:
Originally Posted by Chubler_XL
In your above script you could replace:

Code:
printf "\n"
echo "======="


done < file.txt

with:

Code:
LineTotal=$(($(IFS=+; echo "${TableCount[*]}")))
printf "%*s%*s\n" $(( $((11 + ${#LineTotal}))/2 )) "$LineTotal" $(( $((10 - ${#LineTotal}))/2 )) "|"

done < file.txt

thanks reply ,

I replaced the code as your advice , the result is as below

Code:
No. of "aaa" in Feb 2014 = 
         |Jan 2014 |Feb 2014 |  Total  |
=========|=========|=========|=========|
         |     2   |     4   |     6   |
No. of "bbb" in Feb 2014 = 
         |Jan 2014 |Feb 2014 |Jan 2014 |Feb 2014 |  Total  |
=========|=========|=========|=========|=========|=========|
         |     2   |     4   |     2   |     5   |    13   |

However ,the above data is duplicated , the output should be

Code:
No. of "aaa" in Feb 2014 = 
|Jan 2014 |Feb 2014 |  Total  |
|=========|=========|=========|
|     2   |     4   |     6   |
No. of "bbb" in Feb 2014 =  
|Jan 2014 |Feb 2014 |  Total  |
|=========|=========|=========|
|     2   |     5   |    13   |


Could advise how to modify it ? thanks

Last edited by Don Cragun; 02-13-2014 at 09:37 PM.. Reason: Fix and add CODE tags.
# 5  
Old 02-13-2014
Sorry I didn't pickup on that existing bug in your script.

Change:

Code:
   Total=$(( $Count + $Total ))
while read -d '?' Month Year Count;do

To:

Code:
   Total=$(( $Count + $Total ))
   TableDate=()
   TableCount=()
while read -d '?' Month Year Count;do

# 6  
Old 02-13-2014
thanks reply ,

The current output is as below , it works fine .

Code:
No. of "aaa" in Feb 2014 = 
|Jan 2014 |Feb 2014 | Total |
=========|=========|=========|=========|
| 2 | 4 | 6 |
No. of "bbb" in Feb 2014 = 
|Jan 2014 |Feb 2014 | Total |
=========|=========|=========|=========|
| 2 | 5 | 7 |

one more question , If I Would like the output change to as below , would advise how to modify it ? very thanks
Code:
Month |Jan 2014 |Feb 2014 | Total |
 
=========|=========|=========|=========|
111 | 2 | 4 | 6 |
=========|=========|=========|=========
222 | 2 | 5 | 7 |

Moderator's Comments:
Mod Comment PLEASE use CODE tags when showing sample code, sample input, and sample output!

Moderator's Comments:
Mod Comment Using a second login to get around infraction limits will not be tolerated.

Last edited by Don Cragun; 02-14-2014 at 12:14 AM.. Reason: Add CODE tags again.
# 7  
Old 03-01-2014
Hi All ,

Would you help on my last post ? very thanks
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