Sponsored Content
Top Forums Shell Programming and Scripting Getting week number of the "Monday" Post 302095455 by aigles on Tuesday 7th of November 2006 07:52:58 AM
Old 11-07-2006
Try and adapt the following script (based on cal command output) :
Code:
day=${1:-$(date +%e)}
month=${2:-$(date +%m)}
year=${3:-$(date +%Y)}

#
# Determine week in the month
#

wmonth=${month}
wyear=${year}

cal ${wmonth} ${wyear} | \
awk -v day="$day" '
        /^[[:digit:][:space:]]*$/ && NF>0 {
           if (substr($0, 1, 2) != "  ") wm += 1;
              for (i=1; i<=NF; i++) {
                if ($i+0 == day+0) {
                    print wm;
                    exit;
                 }
              }
           }
    ' | read week

#
# Determine week in previous month
#

if [ -z "${week}" ]
then

   # Previous month/year

   (( wmonth = month - 1 ))
   if (( wmonth == 0 ))
   then
      wmonth=12
      (( wyear -= 1))
   fi

   # Determine week

   cal ${wmonth} ${wyear} | \
   awk '
        /^[[:digit:][:space:]]*$/ && NF>0 {
           if (substr($0, 1, 2) != "  ") wm += 1;
        } 
        END { print wm }
    ' | read week
fi

#
# Display result
#

echo "Date = ${day}-${month}-${year}, Week.Month.Year = ${week}.${wmonth}.${wyear}"

Examples:
Code:
$ wm.ksh
Date =  7-11-2006, Week.Month.Year = 1.11.2006

$ wm.ksh 1 11
Date = 1-11-2006, Week.Month.Year = 5.10.2006


Jean-Pierre.
 

7 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

2. HP-UX

[Solved] crontab issue "day of week"

Is there an issue with running a cron entry like the below entries? 0 2 21 12 2 cd /usr/local/bin;./cksecurity.sh -f /home/theninja/security.dat21 I wanted this to run on Tues at 2am, which it did successfully, however I also had the following entry to run next tuesday that also ran on 12/21.... (3 Replies)
Discussion started by: theninja
3 Replies

3. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

4. Solaris

The slices "usr", "opt", "tmp" disappeared!!! Help please.

The system don't boot. on the screen appears following: press enter to maintenance (or type CTRL-D to continue)...I checked with format command. ... the slices "0-root","1-swap","2-backup" exist. ...the slises "3-var","6-usr" -unassigned. :( (16 Replies)
Discussion started by: wolfgang
16 Replies

5. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

6. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

7. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies
strftime(3C)															      strftime(3C)

NAME
strftime() - convert date and time to string SYNOPSIS
DESCRIPTION
The function converts the contents of a structure (see ctime(3C)) to a formatted date and time string. places characters into the array pointed to by s as controlled by the string pointed to by format. The format string consists of zero or more directives and ordinary characters. A directive consists of a character, an optional field width and precision specification, and a terminating character that determines the directive's behavior. All ordinary characters (including the terminating null character are copied unchanged into the array. No more than maxsize characters are placed into the array. Each directive is replaced by the appropriate characters as described in the following list. The appropriate characters are determined by the program's locale, by the values contained in the structure pointed to by timeptr, and by the environment variable (see below). Directives The following directives, shown without the optional field width and precision specification, are replaced by the indicated characters: Locale's abbreviated weekday name. Locale's full weekday name. Locale's abbreviated month name. Locale's full month name. Locale's appropriate date and time representation. The century number (the year divided by 100 and truncated to an integer) as a decimal number [00-99]. Day of the month as a decimal number [01,31]. Equivalent to the directive string Day of the month as a decimal number [1,31]; a single digit is preceded by a space. Equivalent to the directive string (the ISO 8601:2000 standard date format) Last 2 digits of the week-based year as a decimal number [00,99]. Week-based year as a decimal number (for example, 1977). Equivalent to Hour (24-hour clock) as a decimal number [00,23]. Hour (12-hour clock) as a decimal number [01,12]. Day of the year as a decimal number [001,366]. Month as a decimal number [01,12]. Minute as a decimal number [00,59]. The New-line character. Locale's equivalent of either AM or PM. The time in AM and PM notation; in the POSIX locale this is equivalent to The time in 24 hour notation Second as a decimal number [00,61]. The Tab character. The time in hours, minutes, and seconds The weekday as a decimal number [1(Monday),7]. Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. The week number of the year (Monday as the first day of the week) as a decimal number [01,53]. If the week containing January 1st has four or more days in the new year, then it is considered week 1; otherwise, it is the last week of the previous year, and the next week is week 1. Weekday as a decimal number [0(Sunday),6]. Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. Locale's appropriate date representation. Locale's appropriate time representation. Year without century as a decimal number [00,99]. Year with century as a decimal number. Offset from UTC in the ISO 8601:2000 standards format ( or or by no characters if no timezone is determinable. Time zone name (or by no characters if no time zone exists). The percent (%) character. The following directives are provided for backward compatibility with the directives supported by and the functions. These directives may be removed in a future release. It is recommended that the directives above be used in preference to those below. Locale's combined Emperor/Era name and year (use instead). instead). Locale's Emperor/Era name (use instead). Locale's Emperor/Era year (use instead). If a directive is not one of the above, the behavior is undefined. and give values according to the ISO 8601:2000 standard week-based year. In this system, weeks begin on a Monday and week 1 of the year is the week that includes January 4th, which is also the week that includes the first Thursday of the year, and is also the first week that contains at least four days in the year. If the first Monday of January is the 2nd, 3rd, or 4th, the preceding days are part of the last week of the preceding year; thus, for Saturday 2nd January 1999, is replaced by 1998 and is replaced by 53. If December 29th, 30th, or 31st is a Monday, it and any following days are part of week 1 of the following year. Thus, for Tuesday 30th December 1997, is replaced by 1998 and is replaced by 01. Modified Conversion Specifiers Some conversion specifiers can be modified by the E or O modifier characters to indicate that an alternative format or specification should be used rather than the one normally used by the unmodified conversion specifier. If the alternative format or specification does not exist for the current locale, the behavior will be as if the unmodified conversion specification were used. Alternative numeric symbols refers to those symbols defined by the (see langinfo(5)) in the locale. The locales alternative appropriate date and time representation. The name of the base year (period/Emperor/Era) in the locale's alternative representation. The locale's alternative date representation The locale's alternative time representation. The offset from (year only) in the locale's alternative representation. The full alternative year representation. The day of the month, using the locale's alternative numeric symbols, filled as needed with leading zeros if there is any alternative symbol for zero, otherwise with leading spaces. the day of the month, using the locale's alternative numeric symbols, filled as needed with leading spaces. The hour (24-hour clock) using the locale's alternative numeric symbols. The hour (12-hour clock) using the locale's alternative numeric symbols. The month using the locale's alternative numeric symbols. The minutes using the locale's alternative numeric symbols. The seconds using the locale's alternative numeric symbols. The weekday as a number in the locale's alternative representation (Monday=1). The week number of the year (Sunday as the first day of the week, rules corresponding to using the locale's alternative numeric symbols. The week number of the year (Monday as the first day of the week, rules corresponding to using that locale's alternative numeric symbols. The number of the weekday (Sunday=0) using the locale's alternative numeric symbols. The week number of the year (Monday as the first day of the week) using the locale's alternative numeric symbols. The year (offset from in the locale's alternative representation and using the locale's alternative symbols. Field Width and Precision An optional field width and precision specification can immediately follow the initial of a directive in the following order: The decimal digit string w specifies a minimum field width in which the result of the conversion is right- or left-justified. It is right-justified (with space padding) by default. If the optional flag is specified, it is left-justified with space padding on the right. If the optional flag is specified, it is right-justified and padded with zeros on the left. The decimal digit string p specifies the minimum number of digits to appear for the and directives, and the maximum number of bytes to be used from the and directives. In the first case, if a directive supplies fewer digits than specified by the precision, it will be expanded with leading zeros. In the second case, if a directive supplies more bytes than specified by the precision, excess bytes will truncated on the right. If no field width or precision is specified for a or directive, a default of is used for all but for which is used. EXTERNAL INFLUENCES
Locale The category determines the characters to be substituted for those directives described above as being from the locale. The category determines the interpretation of the bytes within format as single and/or multi-byte characters. The category determines the characters used to form numbers for those directives that produce numbers in the output. If (see langinfo(5)) is defined for the locale, the characters so specified are used in place of the default ASCII characters. If both and is defined for the locale, will take precedence over Environment Variables determines the time zone name substituted for the directive. The time zone name is determined by calling the function which sets the external variable (see ctime(3C)). International Code Set Support Single- and multi-byte character code sets are supported. RETURN VALUE
If the total number of resulting bytes including the terminating null byte is not more than maxsize, returns the number of bytes placed into the array pointed to by s, not including the terminating null byte. Otherwise, zero is returned and the contents of the array are indeterminate. EXAMPLES
If the timeptr argument contains the following values: the following combinations of the category and format strings produce the indicated output: LC_TIME Format String Output ----------------------------------------------------- en_US.roman8 %x Mon, Jul 4, 1988 de_De.roman8 %x Mo., 4. Juli 1988 en_US.roman8 %X 03:09:04 PM fr_FR.roman8 %X 15h09 04 any* %H:%M:%S 15:09:04 any* %.1H:%.1M:%.1S 15:9:4 any* %2.1H:%-3M:%03.1S 15:9 :004 * The directives used in these examples are not affected by the category of the locale. WARNINGS
If the arguments s and format are defined such that they overlap, the behavior is undefined. The function is called upon every invocation of (whether or not the time zone name is copied to the output array). The range of values for ([0,61]) extends to 61 to allow for the occasional one or two leap seconds. However, the system does not accumu- late leap seconds and the structure generated by the functions and (see ctime(3C)) never reflects any leap seconds. Results are undefined if values contained in the structure pointed to by timeptr exceed the ranges defined for the structure (see ctime(3C)) or are not consistent (such as if the element is set to 0, indicating the first day of January, while the element is set to 11, indicating a day in December). AUTHOR
was developed by HP. SEE ALSO
date(1), ctime(3C), getdate(3C), setlocale(3C), environ(5), langinfo(5), thread_safety(5). STANDARDS CONFORMANCE
strftime(3C)
All times are GMT -4. The time now is 03:06 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy