LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-16-2013, 12:48 AM   #16
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705

Hi,
Quote:
Originally Posted by learne View Post
When i executed the above code, i got the output as:

date +%d/%m/%Y --date="yesterday"
16/07/2013
Really? Is the clock on you computer correct? What is the output of the following?
Code:
date
Evo2.
 
Old 07-16-2013, 12:57 AM   #17
learne
LQ Newbie
 
Registered: Jul 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Firerat View Post
What time is it where you are ?

do
Code:
date
date --date="yesterday"
date --date="yesterday"
date: illegal option -- -
Usage: date [-u] [+format]
date [-u] [mmddhhmm[[cc]yy]]
date [-a [-]sss.fff]

i got an error, GNU date is not supporting in my unix prompt.
 
Old 07-16-2013, 01:00 AM   #18
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

what os are you running? AFAIR GNU date has been the default on every Linux box I've used (except for perhaps embedded systems)? Also what is the output of the following?
Code:
date --version
Evo2.
 
Old 07-16-2013, 01:06 AM   #19
learne
LQ Newbie
 
Registered: Jul 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by evo2 View Post
Hi,

what os are you running? AFAIR GNU date has been the default on every Linux box I've used (except for perhaps embedded systems)? Also what is the output of the following?
Code:
date --version
Evo2.
Hi,

I am working on unix machine and on Ksh shell.

when i execute the command. date --version, i got an error

date: illegal option -- -
Usage: date [-u] [+format]
date [-u] [mmddhhmm[[cc]yy]]
date [-a [-]sss.fff]

regards.
 
Old 07-16-2013, 01:12 AM   #20
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
ok, what version of 'date' do you have ?

What *nix are you on?
 
Old 07-16-2013, 01:25 AM   #21
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,
Quote:
Originally Posted by learne View Post
I am working on unix machine and on Ksh shell.
did you realise that you have posted this question in a Linux forum? You may have done better posting in "Other *NIX" forum. Anyway, it seems you've already had your question answered using the cut command.

Cheers,

Evo2.
 
1 members found this post helpful.
Old 07-19-2013, 04:28 AM   #22
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I'm late to the party, but I think we have a few problems that need to be sorted out here.

The first is the ambiguity in the request. It appears the OP wants to take a given input date, in ddmmyyyy format, and use it to get the previous day, output in dd/mm/yyyy format. Is that correct? I'm going to assume so for the rest of this post.

The second problem is that he initially failed to mention that he wants to do this on a Unix machine. This seriously restricts the options available. While the gnu toolset has a reasonable set of functions for converting arbitrary dates, the ones available in most other Unixes do not. Always be sure to mention the environment you're using if it's something different from the assumed Linux+Gnu tools.

Third, since the input timestamp is in a non-standard and hard-to-use format, it's probably necessary to break it up and re-order it into something easier for other tools to understand. If it's possible, I recommend always using the ISO 8601 standard yyyymmdd format instead. It would save a lot of headaches in the long run.

Now, as mentioned, on a Linux system, this is relatively easy. Here's how I'd handle it:
Code:
indate=15072013

day=${indate%??????}
year=${indate#????}
month=${indate#$day}
month=${month%$year}

date -d "$year-$month-$day -1 day" "+%d/%m/%Y"
The first part, which breaks up the string, can be done on any system since it uses posix-standard shell syntax. But the date command won't work, since it relies on the gnu-only -d (or --date, in long form) option.


So on Unix we'd generally have to do something more complex, but what that is depends on the tools available to you. A more powerful language with actual time-conversion functions built in, like perl, would be the best bet. Otherwise you'd probably have to do something like convert the date string to the epoch time, subtract a days worth of seconds from it, then convert it back.


Unfortunately I'm afraid I can't give you any direct solutions at this point. I'm not that familiar with perl, nor is it clear to me what is and isn't supported by the other Unix tools. The OP should clarify what tools he has available, and then perhaps someone with more experience with them can help out.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell scripting question Komelore Linux - Software 3 03-12-2007 04:21 AM
shell scripting question bhert Linux - General 2 01-29-2007 08:55 PM
shell scripting question creolophus Linux - Software 1 10-02-2006 02:13 AM
Shell Scripting Question Onyx^ Linux - General 5 04-27-2004 10:37 AM
Shell Scripting Question jester_69 Programming 13 11-05-2003 06:55 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration