• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

date comman

 
raj esh
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i just want to write a scipt,which should run on 5st day of everymonth(i will put this in cron).

my requiement is want to get the previous month from current date.
suppose today is 5th apr ,then i need to get 1mar to 1apr data records.
suppose today is 6th mar, then i need to get 1feb to 1 mar.

how can i get the previous month by using the date command in unix.
i dont know how to get the previous months from current . please help somebody.



 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would write the script in a language that makes it easy, say Perl. Perl is great for efforts like this.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"comman"?

I realize we're talking about Unix here, but please proof-read your posts.

Also, please check your private messages for an important administrative matter.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at the man page for the Unix date command you'll find all of the ouput sequences.
Once you filter out everything but the month, you can use the expr command to subtract 1



Prints: 3 in April
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:

I realize we're talking about Unix here,...

Good thing he wasn't looking to find the passwd for a given usr!


 
Craig Jackson
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want to keep in mind if the current Month is January(1) then the previous month would be December(12).

You will need to take that into consideration when you develop your algorithm.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, let's do a short travel to the bottom of unix-date (gnu-date?) and back, avoiding perl, 'cause, unreadable code can be done without perl, can't it?

Lesson 1: Format the date with +..., + means 'here comes format' %Y and so on might be looked up with date --help immediately:


That was quiete simple, wasn't it? Visit us tomorrow for the next lesson!

Ok. Lesson 2:
Don't be fixed to today, let's jump directly one month back:

You see us repeating lesson 1 with the format, but -d means 'take a date I provide', and then you put in your date, or you do some arithmetic on the current date. -1month is pretty self explantory, but the missing blank is hard to remember.

Therefore we take a little brake and see us tomorrow.

Ok. Here we go. Lesson 3:
We take a small excursus to string-manipulation in bash.
The output of cmd CMD can be put to a variable cmd with cmd=$(CMD)

first is 8 characters starting at index 0 and we put 01 to the end. We could have used:

but we need lastm though.

Now that was a hard lesson - let's have a brake!

Lesson 4 (allways the hardest one):
Not really that hard, wasn't it? Take first as a date, add 1 month, add 1 negative day, fomat it all the way. Cut off 2010-03- (8 characters) in a second step.

Okay - too much effort. We could use this instead:

This was the only useful thing in all those commands, because it will handle 31 or 30 days, as well as leap years and exceptions from leap years and so on. Ok - Month 01-1=12 was useful before.

Now we got what we need, and put it all together, tomorrow...

Lesson 5: Earning the fruit:


Wow. Now put everything in one line, and you'll got perl.

Some goodies for the weekend:

For more details see:
Our solution was, put to one block of code:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic