Keeping the format ...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Keeping the format ...
# 8  
Old 02-01-2002
Thanks heaps guys. Smilie
Will try to implement it when I go back to work later today.
# 9  
Old 02-27-2002
Guys,

It's working great and I'm using Ksh for the script.

One quick question; is it possible to convert a Jullian date to a formatted date??

Ie : Jullian Date = 032
Formatted Date = 01/02/2002
# 10  
Old 02-27-2002
I know it can be done, but I can't work out the logic for it... I'm not too hot at math logic.

Perderabo, any gems to contribute? Smilie
# 11  
Old 02-27-2002
First of all, we seem to be talking about "Day of Year" here rather than "Julian Date". I know it's pro forma for programmers to mix the two up, but to get this right we have to be precise.

Here is a page that discusses the real meaning of Julian Day. And here is Julian Day Calculator.

Converting a Julian Day Number to a date is rough but it can be done. Converting a Day of Year" to a date is impossible. We must also have the year. We need the year both so that we can display it and to determine if we are dealing with a leap year. Here is a script that takes to parameters, the year and the day of year and converts them to a date:
Code:
#! /usr/bin/ksh

#                ja fe ma ap ma ju ju ag se oc no de
set -A mlength   31 28 31 30 31 30 31 31 30 31 30 31

typeset -Z2 dmonth dday


year=$1
doy=$2

#
#  is this a leap year?
leap=0
if ((!(year%100))); then
	((!(year%400))) && leap=1
else
	((!(year%4))) && leap=1
fi
echo leap = $leap

#
#  set number of days in february
mlength[1]=28
((leap)) && mlength[1]=29
#
#  loop on month
month=0
mdoy=0
while ((month<12 && mdoy<doy)); do
	((oldmdoy=mdoy))
	((mdoy=mdoy+${mlength[month]}))
	((month=month+1))
done

((day=doy-oldmdoy))
dday=$day
dmonth=$month
echo "$dmonth/$dday/$year"
exit 0

# 12  
Old 02-28-2002
My solution....

This is my solution & works great.
Unfortunately I hadn't started gathering stats on the systems this macro is running on since the 16th January.
The scary part is that it took me less than a shift to finish. Smilie
Note: This script is intended to run only on the 1st of any month.
Code:
#!/bin/ksh
typeset -Z3 xcjdate xpjdate xj31date xidl xpeak
typeset -Z2 xrptmth
#######################################################
# >> NOTE: Ensure that you comment ANY amendments. <<
#######################################################
#
# SCRIPT NAME : sarcpurpt
#               Creation of the Monthly CPU Report
#               based on data collected from the
#               previous month.
# ----------------------------------------------------
# PURPOSE:
# This macro is intended to be run via CRONTAB on the
# 1st of every month at 00:05.
#
# Files to be read.
# /amadeus_stats/sar/cpu/cpuJJJ.?
#
# Output File (Report).
# /amadeus_stats/sar/cpu/Reports/MMMYYYY.JJJHHMM
# n.b. - JJJ = Jilian date.
#        MMM = Short Month Name (Jan, Feb, etc.).
#       YYYY = Long Year.
#       HHMM = 24hr Time.
#          ? = CPU Indicator [ 1 , 2 , 3 , 4 ]
#
# ----------------------------------------------------
# AMENDMENTS:
# <DATE> <VER> <WHO> < COMMENTS                      >
# 260202  001   CJY  Initial script.
# 270202  002   CJY  Ready for production.
#
# ----------------------------------------------------
# >> NOTE: Ensure that you comment ANY amendments. <<
#######################################################

# - Environment setup ----------------------------------#

  ## File location/assignments.
  xcpudir=/amadeus_stats/sar/cpu/
  xrptdir=$xcpudir\Reports/
  ## Email Recipients.
  xemails="users@somedomain.com.au"

  ## Long Name Month Listing.
  lngmth="January February March April May June \
          July August September October November December"
  ## Short Name Month Listing.
  shtmth="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"

# - Functions ------------------------------------------#
#
function SendEmail {

  xsub="EVEREST-AU - CPU Monthly Detailed Report - "$2", "$3"."
  mail -s "$xsub" $xemails < $1

}  ## End-of-function SendEmail
#
# - START-OF-SCRIPT ------------------------------------#
##
  xcjdate=`date '+%j'`
  xpjdate=`expr $xcjdate - 1`
  xcurmth=`date '+%m'` ; xcuryr=`date '+%Y'`
  xrptmth=`expr $xcurmth - 1` ; xrptyr=`expr $xcuryr - 1`
  if [ $xrptmth -eq 0 ] ; then
    xshtmth=`echo $shtmth | cut -f12 -d" "`$xrptyr
    xlngmth=`echo $lngmth | cut -f12 -d" "`
  else
    xshtmth=`echo $shtmth | cut -f$xrptmth -d" "`$xcuryr
    xlngmth=`echo $lngmth | cut -f$xrptmth -d" "`
  fi
  if [ $xcjdate -eq 001 ] ; then
    xpjdate=366
    xj31date=`expr $xpjdate - 32`
  else
    xpjdate=`expr $xcjdate - 1`
    xj31date=`expr $xpjdate - 32`
  fi

  ## Set Report File Name.
  xcpurpt=$xrptdir/$xshtmth.`date '+%j%H%M'`
  ##
  ## Create Header.
  echo > $xcpurpt
  echo "SYSTEM: "`hostname`" (EVEREST-AU)" >> $xcpurpt
  echo "CPU Detailed Usage Report - "$xlngmth", "`echo $xshtmth | cut -c4-7` \
       "." >> $xcpurpt
  echo "--------------------------------------------------------\c" >> $xcpurpt
  echo "----------------------------------------------" >> $xcpurpt
  echo "             <----- CPU #1 ----->   <----- CPU #2 ----->\c" >> $xcpurpt
  echo "   <----- CPU #3 ----->   <----- CPU #4 ----->" >> $xcpurpt
  echo "Date       - %Avg %Peak Peak-Time - %Avg %Peak Peak-Time\c" >> $xcpurpt
  echo " - %Avg %Peak Peak-Time - %Avg %Peak Peak-Time" >> $xcpurpt
  while [ $xj31date -lt $xpjdate ] ; do

    for xfile in 1 2 3 4    # Number of CPU files for each day.
      do
        #-- Get Date of CPU report -----------------------------#
        xcpudd=`awk '/SCO_SV/' $xcpudir\cpu$xj31date.$xfile | cut -c36-37`
        xcpumm=`awk '/SCO_SV/' $xcpudir\cpu$xj31date.$xfile | cut -c33-34`
        xcpuyy=`awk '/SCO_SV/' $xcpudir\cpu$xj31date.$xfile | cut -c39-42`
        xcpudate="$xcpudd/$xcpumm/$xcpuyy"
        if [ $xcpumm -eq $xrptmth ] ; then
          xidl=`awk '/SCO_SV/,/Average/' $xcpudir\cpu$xj31date.$xfile |\
                tail -1 | cut -c38-40`
          xidl=`expr 100 - $xidl`
          xpeak=`awk '/SCO_SV/,/Average/' $xcpudir\cpu$xj31date.$xfile | \
               grep ':' | grep -v '%' | grep -v 'mpx restarts' | sort -k 5 | \
               head -1 | cut -c38-40`
          xpeak=`expr 100 - $xpeak`
          xtime=`awk '/SCO_SV/,/Average/' $xcpudir\cpu$xj31date.$xfile |\
               grep ':' | grep -v '%' | grep -v 'mpx restarts' |\
               sort -k 5 | head -1 | cut -c1-8`
          case $xfile in
          1) xcpu1=" "$xidl"   "$xpeak"  "$xtime" -";;
          2) xcpu2=" "$xidl"   "$xpeak"  "$xtime" -";;
          3) xcpu3=" "$xidl"   "$xpeak"  "$xtime" -";;
          4) xcpu4=" "$xidl"   "$xpeak"  "$xtime;;
          esac
        fi
      done
    if [ $xcpumm -eq $xrptmth ] ; then
      IFS=""
      echo $xcpudate" - "$xcpu1" "$xcpu2" "$xcpu3" "$xcpu4 >> $xcpurpt
      IFS=" "
    fi
    xj31date=`expr $xj31date + 1`
  done
  echo "--------------------------------------------------------\c" >> $xcpurpt
  echo "----------------------------------------------" >> $xcpurpt
  echo " End-Of-Report." >> $xcpurpt
  echo >> $xcpurpt
  echo " Nb: Print in Landscape mode suggested." >> $xcpurpt

  chown operator:group $xcpurpt
  chmod 644 $xcpurpt

  SendEmail $xcpurpt $xlngmth `echo $xshtmth | cut -c4-7`

##
# - END-OF-SCRIPT --------------------------------------#

And a sample of the output...
Code:
SYSTEM: www3.travel.com.au (EVEREST-AU)
CPU Detailed Usage Report - January, 2002 .
------------------------------------------------------------------------------------------------------
             <----- CPU #1 ----->   <----- CPU #2 ----->   <----- CPU #3 ----->   <----- CPU #4 ----->
Date       - %Avg %Peak Peak-Time - %Avg %Peak Peak-Time - %Avg %Peak Peak-Time - %Avg %Peak Peak-Time
16/01/2002 -  062   100  05:15:01 -  006   037  12:15:02 -  030   083  12:15:02 -  013   059  12:15:02
17/01/2002 -  070   100  05:15:01 -  006   055  14:15:17 -  030   092  14:01:29 -  012   073  14:01:29
18/01/2002 -  055   099  03:15:01 -  004   019  16:15:01 -  025   077  02:00:00 -  009   036  16:15:01
19/01/2002 -  037   100  04:45:06 -  001   004  05:30:02 -  013   072  02:00:00 -  002   024  05:30:02
20/01/2002 -  036   100  05:00:01 -  001   024  13:30:00 -  014   074  02:00:00 -  003   031  13:30:00
21/01/2002 -  054   100  05:00:01 -  004   018  11:00:00 -  025   075  02:00:01 -  008   028  15:45:00
22/01/2002 -  065   099  02:45:00 -  013   049  16:45:00 -  037   075  02:00:01 -  020   058  16:45:00
23/01/2002 -  064   098  02:45:00 -  005   030  11:45:00 -  033   077  02:00:00 -  012   045  11:45:00
24/01/2002 -  064   098  02:45:00 -  005   043  15:00:02 -  032   086  15:00:02 -  011   063  15:00:02
25/01/2002 -  064   098  02:45:00 -  005   026  10:30:01 -  031   076  02:00:00 -  011   044  10:30:01
26/01/2002 -  045   099  04:45:01 -  001   009  05:30:01 -  018   074  02:00:01 -  004   029  05:30:01
27/01/2002 -  040   098  02:45:01 -  001   017  17:45:00 -  016   076  05:30:01 -  004   027  02:00:00
28/01/2002 -  043   099  05:00:01 -  001   006  02:00:00 -  017   074  02:00:00 -  004   025  02:00:00
29/01/2002 -  062   099  02:45:00 -  005   038  16:15:00 -  029   079  16:15:00 -  011   056  16:15:00
30/01/2002 -  061   099  02:45:00 -  005   026  12:15:01 -  029   076  02:00:00 -  011   046  12:15:01
------------------------------------------------------------------------------------------------------
 End-Of-Report.

# 13  
Old 02-28-2002
Ahh, just a smal oversite with the above script - an error.

The line ...
while [ $xj31date -lt $xpjdate ] ; do
should actually read ...
while [ $xj31date -le $xpjdate ] ; do

I only stpotted the error when the emails were missing the last day of the month - doh!
# 14  
Old 03-05-2002
Quote:
Originally posted by Perderabo

Converting a Julian Day Number to a date is rough but it can be done.
Yep. Check out a previous thread where I posted a script to convert to & from julian date values.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Replace some strings keeping others

I want to replace strings in test2 according to test1 table. In doing so, I`m losing records that I dont need to replace, please suggest modifications. what i have $ cat > test1 a b c d   $ cat > test2 a a a d d   what i tried $ awk ' BEGIN {FS=OFS=" "} FNR==NR{a=$2;next}... (2 Replies)
Discussion started by: senhia83
2 Replies

2. Shell Programming and Scripting

Keeping the number intact

Currently I have the following to separate the numeric values. However the decimal point get separated. ls -lrt *smp*.cmd | awk '{print $NF}' | sed 's/^.*\///' | sed 's/\(*\)/ & /g' As an example on the files n02-z30-dsr65-terr0.50-dc0.05-4x3smp.cmd... (8 Replies)
Discussion started by: kristinu
8 Replies

3. Shell Programming and Scripting

Replace date value with another value keeping all as is

Hi forum. How do I change the following date value with another value (while keeping the rest of the line) using sed? The date values can change so I need a general sed command to change the date value within the first quotation marks only. Date values will be coming from 2 different files.... (2 Replies)
Discussion started by: pchang
2 Replies

4. Shell Programming and Scripting

Keeping last part

Hello, Sorry for the poor tilte but I still don't know how to this. Here is my problem. I have to huge log file. In this log file I can know where is stored all my files. As I have to get a reporting of of files I only need to keep the file name but I don't know how to do it. I hope you... (5 Replies)
Discussion started by: Aswex
5 Replies

5. Shell Programming and Scripting

Help with file editing while keeping file format intact

Hi, I am having a file which is fix length and comma seperated. And I want to replace values for one column. I am reading file line by line in variable $LINE and then replacing the string. Problem is after changing value and writing new file temp5.txt, formating of original file is getting... (8 Replies)
Discussion started by: Mruda
8 Replies

6. Solaris

keeping a process alive ?

Hello guys, I have one script running that I need to keep it running 24x7 so I'd like to know how can I implement a sort of monitoring process I mean if for some reason this process dies somehow it gets automatically started again. Thanks. (8 Replies)
Discussion started by: cerioni
8 Replies

7. Red Hat

keeping systems updated

I have several RHEL systems that are on an isolated network so I can't run up2date or yum directly on them. What is the best way to keep these systems updated and patched? Thanks (4 Replies)
Discussion started by: wazzu62
4 Replies

8. AIX

Script Keeping Track of Itself

Hi All We have a WEB Based application running on the IBM AIX server. There is a EOD Job which runs a UNIX script containing EOD Jobs. Say If any job fails then we have to explicitly comment out the jobs which were successfully executed and then re run the same. Is there any was by which we... (7 Replies)
Discussion started by: Prashantckc
7 Replies

9. Solaris

keeping history of command

hi can anyone tell me how or where to set to enable history of command keyed in to be logged? so that it can be used or traced later. thanks (3 Replies)
Discussion started by: legato
3 Replies
Login or Register to Ask a Question