LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 01:48 AM   #16
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288

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, 01: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, 02:00 AM   #18
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
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, 02: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, 02:12 AM   #20
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

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

What *nix are you on?
 
Old 07-16-2013, 02:25 AM   #21
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
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, 05:28 AM   #22
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 05:21 AM
shell scripting question bhert Linux - General 2 01-29-2007 09:55 PM
shell scripting question creolophus Linux - Software 1 10-02-2006 03:13 AM
Shell Scripting Question Onyx^ Linux - General 5 04-27-2004 11:37 AM
Shell Scripting Question jester_69 Programming 13 11-05-2003 07:55 PM


All times are GMT -5. The time now is 08:34 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration