monthly calculation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting monthly calculation
# 1  
Old 08-19-2008
monthly calculation

pls can anyone help me with this script, the script is below, i need the script to get the previous month result every new month , the problem is that the loop has to be automated to always calculate for previous month .


a=`date "+%Y"` #this year to be used
b=$(date "+%Y%m" --date='49 days ago') #previous month with year to be used
for i in 49 48 47 46 45 44 43 42 41 40 39 38 37 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19
do
g=$(date "+%Y%m%d" --date=''$i' days ago') # n days ago to be used
h="lic"$g*405*.txt #the file name
cd /home/file/lic$a/$b #the directory

#---------- Count information----------#
cat $h|awk -F '|' '$4 ~ /^700[0]/ && $9 == '0' { print $0 }'|awk -F '|' '{ b = count[$1]++; }''{ print b }' >> result$b.txt

done
# 2  
Old 08-20-2008
Maybe this will help....
Code:
$ cat loopr
#! /usr/bin/ksh
#
# get current month and year
date '+%Y %m' | read year month
month=${month#0}   # drop leading zero

#
# back up one month
((month=month-1))
if ((!month)) ; then
       month=12
       ((year=year-1))
fi
echo $year $month

#
# compute last day of month
leap=0
if ((!(year%100))); then
       ((!(year%400))) && leap=1
else
       ((!(year%4))) && leap=1
fi
#                 ja fe ma ap ma jn jl ag se oc no de
set -A mlength xx 31 28 31 30 31 30 31 31 30 31 30 31
((leap)) && mlength[2]=29

last=${mlength[month]}

#
# loop
typeset -Z2 fmonth fday
day=1
while ((day<=last)) ; do
       fmonth=$month
       fday=$day
       h=lic${year}${fmonth}${fday}\*405\*.txt
       echo $h
       ((day=day+1))
done
exit 0
$ ./loopr
2008 7
lic20080701*405*.txt
lic20080702*405*.txt
lic20080703*405*.txt
lic20080704*405*.txt
lic20080705*405*.txt
lic20080706*405*.txt
lic20080707*405*.txt
lic20080708*405*.txt
lic20080709*405*.txt
lic20080710*405*.txt
lic20080711*405*.txt
lic20080712*405*.txt
lic20080713*405*.txt
lic20080714*405*.txt
lic20080715*405*.txt
lic20080716*405*.txt
lic20080717*405*.txt
lic20080718*405*.txt
lic20080719*405*.txt
lic20080720*405*.txt
lic20080721*405*.txt
lic20080722*405*.txt
lic20080723*405*.txt
lic20080724*405*.txt
lic20080725*405*.txt
lic20080726*405*.txt
lic20080727*405*.txt
lic20080728*405*.txt
lic20080729*405*.txt
lic20080730*405*.txt
lic20080731*405*.txt
$

# 3  
Old 08-21-2008
Thanks Perderabo for your reply , pls can you let me know what the below is meant for in the script

set: -A

typeset: -Z

presently the script gives me error because of this options

error:
set: -A: invalid option
set: usage: set [--abefhkmnptuvxBCHP] [-o option] [arg ...]

typeset: -Z: invalid option
typeset: usage: typeset [-afFirtx] [-p] name[=value] ...
# 4  
Old 08-21-2008
set -A defines an array and typeset -Z2 define a two position field that shold have leading zeros.

Note the first line:
#! /usr/bin/ksh

This is a ksh script. You are probably trying to run it in bash.
# 5  
Old 08-21-2008
ok, am not familiar with ksh though, but runing it in ksh gives the below error

-1
./su.sh[28]: mlength: subscript out of range
# 6  
Old 08-21-2008
i think just by changing TZ we can go to any date..
why aren't you using that??
# 7  
Old 08-21-2008
many thanks Perderabo for the brilliant solution and thanks vidyadhar .
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Tar archives monthly

Hi, I want to archive files by month, is there anyway of this code looks better? find /tmp/w/ -type f -newermt '2014-01-01' ! -newermt '2014-02-01' | xargs tar -czvf files01.tar find /tmp/w/ -type f -newermt '2014-02-01' ! -newermt '2014-03-01' | xargs tar -czvf files02.tar find... (9 Replies)
Discussion started by: prpkrk
9 Replies

2. Shell Programming and Scripting

Archiving a log file on monthly basis

OS : RedHat Linux 6.2 Shell : Bash Our application write messages, warnings,..etc to the following log file . /app/cms/diagnostics/logs/cms_messages.log This file has become huge now. I want this file to be archived on monthly basis using a small shell script. ie. On the 1st day of... (1 Reply)
Discussion started by: omega3
1 Replies

3. Shell Programming and Scripting

help with a script to have monthly sar reports

Hi I am still learning shell scripting, so for complex stuff, I need help. I would like to have a script that produces sar monthly reports, so that I can produce a graph from it! The idea is to use /var/adm/sa/<dir of sa files>Looking to hear from you. regards (4 Replies)
Discussion started by: fretagi
4 Replies

4. UNIX for Advanced & Expert Users

Autosys JOB on monthly basis

Dear All, Can someone tell me how do I setup autosys job where it needs to execute on monthly basis that too on 1st day of the month. Thanks. (3 Replies)
Discussion started by: shahnazurs
3 Replies

5. Shell Programming and Scripting

Execution problem with crom for monthly

I have to setup cron job for monthly and the month may be 30 or 31 Thanks, (2 Replies)
Discussion started by: lakshmikant
2 Replies

6. Post Here to Contact Site Administrators and Moderators

monthly membership??

Hi, Do we have monthly membership (VIP) in our forum? if not, do we have any plans for it? Regards, (1 Reply)
Discussion started by: clx
1 Replies

7. UNIX for Advanced & Expert Users

shedule monthly reboot on first sunday

Hello, I looked around but can't find a clear answer on this. Is there a way to shedule a box to reboot say on the first sunday of every month? Does it involve the cron.monthly file somehow, I googled and can't find how those cron.weekly, cron.monthly files function... This would be for Suse 9... (8 Replies)
Discussion started by: benefactr
8 Replies
Login or Register to Ask a Question