Converting date in terminal

BadKnees

Registered
I dont understand why this command gives an error:

date -j -f "%Y%m%d%H%M%S" "20091101201023" "+%s"

Failed conversion of ``20091101201023'' using format ``%Y%m%d%H%M%S''
date: illegal time format
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]


But this i ok:

date -j -f "%Y %m %d %H %M %S" "2009 11 01 20 10 23" "+%s"

1257106223

The commands in Mac Os X are a bit different than in Linux that im am used to. Makes it somewhat difficult to script in the shell.

20091101201023 is the format of the date that i get from a file and i want to convert it in a script to seconds since epoc among other things.

Is it possible with the date command? Or is there some replacement for the date command in the terminal?
 
Ok this is a bit off on a tangent maybe, but what if you change the input string you get to match what the date command expects? Here's an example of one way to do just that, using sed (line wrapped with a trailing backslash for clarity):

Code:
echo 20091101201023 | \
sed 's/\([0-9][0-9]\)/\1 /g;s/\(^..\) /\1/;s/ $//'

You could store the output of this command in another variable, and then pass that to the date command....

The sed expression above has three sections to it which accomplish the following:

(1) s/\([0-9][0-9]\)/\1 /g
This appends a space after every two digits (as a result, also adds a space at the end of the string)

(2) s/\(^..\) /\1/
Removes the space after the first two digits only, turning "20 09" back into 2009

(3) s/ $//
Removes the trailing space at the end of the string
 
Last edited:
Ahh, good one.

I did not think of that.

The small differences between linux and mac osx terminal commands are truly annoying.

route does not display routes, you have to use netstat
find needs an argument
Most of the time flags cannot be specified last. For example just to be safe i delete recursivly with: rm /User/crap -rf, so that i don't accidently press enter right after the / , but in Mac os x the -rf must be between the rm and the dir.

etc etc
 
The small differences between linux and mac osx terminal commands are truly annoying.
Not to nitpick, but this is because Mac OS X is UNIX (not Linux), and Linux is Linux (not UNIX).

Mac OS X is based upon Berkeley's FreeBSD UNIX operating system. Linux is not based upon any UNIX distribution (although Linus Torvalds may have used UNIX as inspiration when creating Linux).

While syntactically similar, UNIX and Linux are two, completely separate, mutually exclusive and binary-incompatible systems.

To get quite technical, UNIX isn't even an operating system -- it's a "standard" in that any operating system can be UNIX as long as it conforms to a very long list of UNIX standards. Linux isn't even an operating system -- it's a kernel, which, by itself, isn't even close to being an operating system.

UNIX is as different from Linux as Windows is to a banana.
 
My Mac OSX Terminal, upon launching automatically goes to MySql, when I want it to go to the prompt. is there a special file I can edit, i.e .profile, that would clear this?
 
Back
Top