need help scripting a date when the date is 1st to 9th


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need help scripting a date when the date is 1st to 9th
# 1  
Old 07-10-2012
need help scripting a date when the date is 1st to 9th

I'm new to scripting, I am trying to get the current month and date in my script I have this line
Code:
MYDATE=`date '+%b %d'`

this works for dates 10-31, but it returns a 09 for the 9th, where the file name has a 9,

I tried
Code:
MYDATE=`date '+%b %_d'`

but that returns Jul %_d

I need it to return Jul 9 and not Jul 09

I just need to get the current month date and pull any lines that match it from a log file

Moderator's Comments:
Mod Comment Please view this code tag video for how to use code tags when posting code and data.

Last edited by vbe; 07-10-2012 at 09:43 AM..
# 2  
Old 07-10-2012
Code:
DATE(P) 		   POSIX Programmer's Manual		       DATE(P)

       %e     Day of the month as a decimal number [1,31] in a two-digit field
	      with leading space character fill.

Will this do? Or you need to trim the space too?
This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 07-10-2012
Try this, it should lose the extra space from the %e.
Code:
MYDATE=`date '+%b %e' | tr -s " "`

However if it turns out that you need that extra space (As you often do when looking at system logs):
Code:
MYDATE="`date '+%b %e'`"


Pity you didn't post this yesterday!

Last edited by methyl; 07-10-2012 at 09:57 AM.. Reason: mispaste
This User Gave Thanks to methyl For This Post:
# 4  
Old 07-10-2012
Here is a way of testing: (linux)
Code:
date -d " 1 day ago" '+%b %e'
Jul  9

This User Gave Thanks to vbe For This Post:
# 5  
Old 07-10-2012
Thank you this worked like a charm

Code:
 
MYDATE="`date '+%b %e'`"


Last edited by rsurwill; 07-10-2012 at 10:06 AM..
# 6  
Old 07-10-2012
And for unix in timezones near the meridian (not an accurate way of getting yesterday's date):

Code:
TZ=GMT+24 date '+%b %e'|tr -s " "
Jul 9

TZ=GMT+24 date '+%b %e'
Jul  9

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Answers to Frequently Asked Questions

Compare date in .txt with system date and remove if it's lesser than system date

I m working on shell scripting and I m stuck where in my .txt file there is column as expiry date and I need to compare that date with system date and need to remove all the rows where expiry date is less than system date and create a new .txt with update. (1 Reply)
Discussion started by: Stuti
1 Replies

2. UNIX for Beginners Questions & Answers

Compare date in .txt with system date and remove if it's lesser than system date

Can someone help me with the code wherein there is a file f1.txt with different column and 34 column have expiry date and I need to get that and compare with system date and if expiry date is <system date remove those rows and other rows should be moved to new file f2.txt . I don't want to delete... (2 Replies)
Discussion started by: Stuti
2 Replies

3. Shell Programming and Scripting

Date: invalid date trying to set Linux date in specific format

i try to set linux date & time in specific format but it keep giving me error Example : date "+%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" or date +"%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" keep giving me this error : date: invalid date ‘19-01-2017 00:05:01' Please use CODE tags... (7 Replies)
Discussion started by: umen
7 Replies

4. Shell Programming and Scripting

Bash Scripting with date format conversion

I have a script below and wanted to change the output into three different file format (3 separate script) #!bin/bash #input file format postwrf_d01_20131206_0600_f08400.grb2 #postwrf_d01_YYYYMMDD_ZZZZ_f0HHHH.grb2 #zzzz= 0000,0600,1200,1800 (in UTC) #HHHH=00000,00600,01200,01800 ..ect (in... (1 Reply)
Discussion started by: cumulus_255
1 Replies

5. Shell Programming and Scripting

Shell Scripting Date manipulation

Hi Experts, i have date as inputdate=01/01/2013,how to get the previous date from this date and also first day's date of the month. example: inputdate=01/06/2013 previousdate=31/05/2013 firstdate=01/05/2013 how can i get solution to this. my unix is not supporting GNU Dates ... (0 Replies)
Discussion started by: learner24
0 Replies

6. Shell Programming and Scripting

Converting a date to friday date and finding Min/Max date

Dear all, I have 2 questions. I have a file with many rows which has date of the format YYYYMMDD. 1. I need to change the date to that weeks friday date(Ex: 20120716(monday) to 20120720). Satuday/Sunday has to be changed to next week friday date too. 2. After converting the date to... (10 Replies)
Discussion started by: 2001.arun
10 Replies

7. Shell Programming and Scripting

Date One Week Ago From Given Date, Not From Current Date

Hi all, I've used various scripts in the past to work out the date last week from the current date, however I now have a need to work out the date 1 week from a given date. So for example, if I have a date of the 23rd July 2010, I would like a script that can work out that one week back was... (4 Replies)
Discussion started by: Donkey25
4 Replies

8. AIX

Scripting AIX date output !!

Hi, I am writing a korn shell script in AIX. I want to pass the date as a parameter to script and inside it should convert the format to +%a" "%h" "%d" "%Y. For e.g., a parameter passed as "20100301" should be converted to "Mon Mar 01 2010". I can easily do this with AIX "date" option for... (2 Replies)
Discussion started by: shibajighosh
2 Replies

9. Shell Programming and Scripting

ksh scripting ... get previous date

hi all, i am developing a korn shell script. One of the requirement is to generate the date, which is 7 days before the current date, and create a folder with that date. So for ex: if the time command returns 13 Feb 2009, I should be able to generate 6 Feb 2009, and assign that to a variable. ... (4 Replies)
Discussion started by: allah_waris45
4 Replies

10. Shell Programming and Scripting

Date Scripting Question

I am trying to append a portion of a file with a date on it that changes daily to a temp file. Here are my commands: set DATE = ('date +%Y%m%d_sum.log') cat $HOME/right/$DATE > /export/home/temp.txt Here is the error I keep receiving: cat: cannot open /temp/usr/nedaniel/right/date... (6 Replies)
Discussion started by: BigDawg32
6 Replies
Login or Register to Ask a Question