How can I get first day of a particular month?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can I get first day of a particular month?
# 1  
Old 10-10-2014
How can I get first day of a particular month?

Hi,

I am new to shell scripting and I have a requirement of getting first day of specific month.

eg i need 1st day of oct 2014

Code:
 October 2014
 S  M Tu  W Th  F  S
               1  2  3   4
 5  6  7    8  9 10  11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

output should be: Wed

Please suggest.

Last edited by Franklin52; 10-10-2014 at 07:29 AM.. Reason: Please use code tags
# 2  
Old 10-10-2014
Did you search these fora? There should be plenty of examples / solutions to your problem... e.g. sth along this line (untested):
Code:
cal 10 | awk 'BEGIN {split ("Sun Mon Tue Wed Thu Fri Sat", DAY)} NR==3 {print DAY[8-NF]}'

# 3  
Old 10-10-2014
Quote:
Originally Posted by simsim90
Hi,

I am new to shell scripting and I have a requirement of getting first day of specific month.

eg i need 1st day of oct 2014

October 2014
S M Tu W Th F S
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

output should be: Wed

Please suggest.
Hello simsim90,

Welcome to forum. Here is an example for same. Lets say we need to see month 2nd for year 1990, so here is an example for same.

Code:
cal 2 1990
   February 1990
Su Mo Tu We Th Fr Sa
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28

So code to get 1st date of the month is as follows.
Code:
cal | awk 'NR==2{for(u=1;u<=NF;u++){A[++p]=$u}} NR==3 && NF<=7 {gsub(/[[:space:]]/,X,$0);val=length($0);actual_value=8-val;print A[actual_value]}'

Output will be as follows.
Th

Another example for current month(Which is October).
Code:
cal | awk 'NR==2{for(u=1;u<=NF;u++){A[++p]=$u}} NR==3 && NF<=7 {gsub(/[[:space:]]/,X,$0);val=length($0);actual_value=8-val;print A[actual_value]}'
OR
cal | awk 'NR==2{for(u=1;u<=NF;u++){A[++p]=$u}} NR==3 && NF<=7 {gsub(/[[:space:]]/,X,$0);print A[8-length($0)]}'
 
Output will be as follows.
We

Thanks,
R. Singh

Last edited by RavinderSingh13; 10-10-2014 at 07:31 AM.. Reason: Using icode instead of code for output
# 4  
Old 10-10-2014
Try something like this

Script

Code:
#!/bin/bash

first_day(){ date -d "01/01/$1 + $(($2-1)) month" +"%d %B %Y %a" ;}
last_day(){  date -d "01/01/$1 + $2 month -1 day" +"%d %B %Y %a" ;}

# call function
first_day 2014 10
last_day 2014 10

first_day 2014 12
last_day 2014 12

first_day 2015 1
last_day 2015 1

Calender
Code:
[akshay@nio tmp]$ cal 10 2014
    October 2014    
Su Mo Tu We Th Fr Sa
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

[akshay@nio tmp]$ cal 12 2014
    December 2014   
Su Mo Tu We Th Fr Sa
    1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

[akshay@nio tmp]$ cal 1 2015
    January 2015    
Su Mo Tu We Th Fr Sa
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

Output
Code:
[akshay@nio tmp]$ bash date.sh
01 October 2014 Wed
31 October 2014 Fri
01 December 2014 Mon
31 December 2014 Wed
01 January 2015 Thu
31 January 2015 Sat

# 5  
Old 10-11-2014
if you can use ksh93 (Korn Shell 93):
Code:
$ printf "%(%a)T\n" "first day oct 2014"
Wed
$ printf "%(%a)T\n" "first day mar 2014"
Sat

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Julian day to dates in YEAR-MONTH-DAY

hello, I have many files called day001, day002, day003 and I want to rename them by day20070101, day20070102, etc. I need to do it for several years and leap years as well. What is the best way to do it ? Thank you. (1 Reply)
Discussion started by: Ggg
1 Replies

2. Shell Programming and Scripting

Script to counting a specific word in a logfile on each day of this month, last month etc

Hello All, I am trying to come up with a shell script to count a specific word in a logfile on each day of this month, last month and the month before. I need to produce this report and email it to customer. Any ideas would be appreciated! (5 Replies)
Discussion started by: pnara2
5 Replies

3. Shell Programming and Scripting

Code creates day 32 instead of 1st day of next month.

I am using the code below modified from a post I saw here regarding having the script write out future dates. The problem is that instead of making 8/1 it makes 7/32! Please help! yy=`date +%Y` mm=`date +%m` dd=`date +%d` echo "Today is : $yy $mm $dd" #!/usr/bin/ksh date '+%m... (5 Replies)
Discussion started by: libertyforall
5 Replies

4. Shell Programming and Scripting

Script to find previous month last day minus one day timestamp

Hi All, I need to find the previous month last day minus one day, using shell script. Can you guys help me to do this. My Requirment is as below: Input for me will be 2000909(YYYYMM) I need the previous months last day minus 1 day timestamp. That is i need 2000908 months last day minus ... (3 Replies)
Discussion started by: girish.raos
3 Replies

5. Shell Programming and Scripting

determine last day of the month

In a ksh script how do I determine if I am on the last day fo the month ? thanks (4 Replies)
Discussion started by: hanton
4 Replies

6. Shell Programming and Scripting

how to assign the first day of the next month

Hi everybody, I am very new in shell and this is my first post :). i am trying to write a script in which i want to decide some begin dates. this dates will be used by another shell scripts. i would like to assign: 1- a variable which is a = sysdate 2- another variable b = the first day of... (6 Replies)
Discussion started by: rosalinda
6 Replies

7. Shell Programming and Scripting

Get Last working day of the month

Hi I need a script to get "Last working day of the month". I will pass the month and year as parameters and i need to get the last working date. Ex for June 2008 the last working day is 30th its monday. for August 2008 the last working day is 29th and it is Friday. ie the last working... (6 Replies)
Discussion started by: manmarirama
6 Replies

8. Shell Programming and Scripting

The last day of the last month

I am a beginner in unix. Can anyone tell me how to get the last day of the last month in 'yyyymmdd' format? eg. today is 20050909, I want to get 20050831. Thanks in advance. (2 Replies)
Discussion started by: emily79
2 Replies

9. Shell Programming and Scripting

last day of the month

can any one please tell me how can I get the last day of the month Thanks (4 Replies)
Discussion started by: aya_r
4 Replies

10. Shell Programming and Scripting

Write a shell script to find whether the first day of the month is a working day

Hi , I am relatively new to unix... Can u pls help me out to find out if the first day of the month is a working day ie from (Monday to Friday)...using Date and If clause in Korn shell.. This is very urgent. Thanks for ur help... (7 Replies)
Discussion started by: phani
7 Replies
Login or Register to Ask a Question