[SOLVED] how to extract the day and month from a user supplied date ?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers [SOLVED] how to extract the day and month from a user supplied date ?
# 1  
Old 09-19-2012
[SOLVED] how to extract the day and month from a user supplied date ?

Hi All ,

I am trying to extract the day and month from a user supplied date . But not able .
Please help .
This is what I am trying to do , I have followed other articles in this scenario.

Code:
userdate=$1  
echo "Script parameter userdate is $userdate"

The output of this is in this format 2012/09/19 00:00:00
Now I want to get the day (ie, 19) and month (ie 09) . How can I get those values?
I tried this
Code:
myDate=`$userdate -u +%d`
myMonth=`$userdate -u +%m`

But thats not displaying any value . Please let me know what is wrong .

Thanks
Megha
# 2  
Old 09-19-2012
If format remains standard then u can use..

Code:
 
echo "2012/09/19 00:00:00" |awk -F"[ /:]" '{print "Month:"$2"\nDate:"$3}'

# 3  
Old 09-19-2012
Hi vidyadhar85

Thank you for the reply.
But it does not display the month and day .
I get blank values.
Please let me know if I am missing something here.

Megha
# 4  
Old 09-19-2012
post your full code please....
# 5  
Old 09-19-2012
Code:
userdate=$1
echo "Script parameter user date = $userdate"

I tried this and get blank values for month and day

Code:
echo "2012/09/19 00:00:00" |awk -F"[ /:]" '{print "Month:"$2"\nDate:"$3}'

I also tried the following and get blank values for month and day
Code:
echo "$userdate" |awk -F"[ /:]" '{print "Month:"$2"\nDate:"$3}'

# 6  
Old 09-19-2012
What's your system? Not all systems support a regex for awk's field separator. GNU awk does, few others do.

You can do this in any Bourne shell:

Code:
USERDATE="2012/09/19 00:00:00"

IFS=":/ " read YYYY MM DD HH MM SS <<EOF
$USERDATE
EOF

echo $YYYY
echo $MM
echo $DD
echo $HH
echo $MM
echo $SS

Note that IFS is ':', '/', and ' ' there, space included. IFS for read (and shell splitting in general) is just a list of characters.

Last edited by Corona688; 09-19-2012 at 12:46 PM..
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 09-19-2012
Hi Coronoa688,

Thank you for the reply. I am on SunOS 5.10
I have this in a korn shell script
Code:
USERDATE=$1

echo "Script parameter User date = $USERDATE"

The output is Script parameter User date = 2012/09/19 00:00:00

But for the following
Code:
IFS=":/ " read YYYY MM DD HH MM SS <<EOF
$USERDATE
EOF

echo $YYYY
echo $MM
echo $DD
echo $HH
echo $MM
echo $SS

The output is
2012
00
19
00
00
00

Why is the month 00 ? It should have been 09
Please look into this .

Thanks
Megha

---------- Post updated at 12:35 PM ---------- Previous update was at 12:03 PM ----------

Hi Coronoa688,

I figured out whats going on .
Code:
IFS=":/ " read YYYY MM DD HH MM SS <<EOF $USERDATE EOF  echo $YYYY echo $MM echo $DD echo $HH echo $MM echo $SS

I changed this to
Code:
IFS=":/ " read YYYY MON DD HH MM SS <<EOF $USERDATE EOF  echo $YYYY echo $MON echo $DD echo $HH echo $MM echo $SS

and it worked. The issue was with the MM for the second time. It reads 00 for the second time , and displays 00.

Thanks a lot Coronoa688 .

Last edited by megha2525; 09-20-2012 at 04:47 PM..
This User Gave Thanks to megha2525 For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to get first & last day of a month from current date?

Hi, I need the first & last day of a month from any given date. For better understanding, if i need to back-fill data for date 07/20/2019 i.e July 20 2019, i need the first & last day has 07/01/2019 - 07/31/2019. FYI: I'm using GIT BASH terminal. sample code: export DT=$(date --date='6 days... (2 Replies)
Discussion started by: Rocky975583
2 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Using date command, getting previous day

Legends, i need to get previous day using date command. Can you please help. sdosanjh:/home> date +%m%d%y 011514 i tried -d '-1 day' but it is not working (5 Replies)
Discussion started by: sdosanjh
5 Replies

3. AIX

Need to get the next day's date of the user entered date

I need to get the next day's date of the user entered date for example: Enter date (yyyy/mm/yy): 2013/10/08I need to get the next day's date of the user entered date Desired Output: 2013/10/09Though there are ways to achieve this is Linux or Unix environment (date command) ,I need to... (1 Reply)
Discussion started by: rpm120
1 Replies

4. 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

5. Shell Programming and Scripting

How to extract log data based on current date and month ?

Hi Gurus, I'm using HP-UX B.11.23 operating system. I've been trying to extract this log info based on the current date and month, but was having some issues as the date column which on the 4th column has a comma and the 5th column has a dot tied to it. Here is the output from my shut... (5 Replies)
Discussion started by: superHonda123
5 Replies

6. Shell Programming and Scripting

Get yesterday's date in year-month-day format?

Dear All, Actually, i'm doing some reporting job and i need to pass yesterday's date in Year-Month-Day format(e.g. 2009-06-10) to another program for generating 2009-06-10 report. to get today's date, it's easy to just date '+%Y%m%d' , but no idea how can i get this kind of format for... (2 Replies)
Discussion started by: tiger2000
2 Replies

7. Shell Programming and Scripting

needs to display month for previous day date

Hello, I wanted to display the month for previous day date. Like, today date is 18-Nov-2008. So the previous date is 17-Nov-2008. The output should be November. If the today date is 1-DEC-2008, then output should be NOVEMBER. If the today date is 1-JAN-2008, then output should be DECEMBER.... (4 Replies)
Discussion started by: govindts
4 Replies

8. Shell Programming and Scripting

single digit for day and month on date

hi all, how do i format the date command so it displays day and month in single digits i.e 8 instead of 08 ?? am using the command (in a ksh) : date +%D output i get is 10/08/08 thanks in advance. (5 Replies)
Discussion started by: cesarNZ
5 Replies

9. Shell Programming and Scripting

Cron to run first day of month to calculate date 3 months ago

Hi, I would like to find out how can i calculate a date which is 3 months ago. I intend to run a cron job on the 1st of every month, and calculate the month 4 months earlier from the date. For example, if today's date is 1st May 2007, i would like to return 012007( January 2007). i can get... (1 Reply)
Discussion started by: new2ss
1 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