Reputation: 125
I have a text file with the following dates in the format dd/mm/yyyy
:
14/09/1992
15/04/1983
15/09/1961
02/06/1979
14/12/1971
08/02/2001
19/06/1999
14/09/1969
31/08/2002
30/07/1980
25/04/1973
03/01/1992
I want to convert this into the YYYY/mm/dd
format in a script. I was planning to use the date command to do so:
date -f tempDate +%Y/%m/%d
but the thing is, it does not recognize the English-style dates and shows the following error as i believe it is reading it as mm/dd/yyyy
when i don't want it to:
date: invalid date `14/09/1992'
is there another way of doing this? or is there a way i can change the order of the date format?
also, I need this script to work on a server that I am not a sysadmin for.
Upvotes: 1
Views: 5495
Reputation: 139
If you are using Windows you can also change your regional settings to change the date format on your computer as a whole and in particular your .csv-file:
Win7:
1./ Go to Start button 2./ Choose Control panel 3./ Clock, language and region. 4./ Change the date, time and number format.
Upvotes: 0
Reputation: 85795
If the file is as described why not just reverse the columns with awk
:
$ awk 'BEGIN{FS=OFS="/"}{print $3,$2,$1}' file
1992/09/14
1983/04/15
1961/09/15
1979/06/02
1971/12/14
2001/02/08
1999/06/19
1969/09/14
2002/08/31
1980/07/30
1973/04/25
1992/01/03
Edit:
Just as easy in awk
with multiple field separator:
$ awk -F[/,] '{print $1","$2","$5"/"$4"/"$3}' <<< "name,number,14/09/1992"
name,number,1992/09/14
Upvotes: 5
Reputation: 753970
On Mac OS X at least, the -f
option to date is documented as:
-f
Use input_fmt as the format string to parse the new_date provided rather than using the default[[[mm]dd]HH]MM[[cc]yy][.ss]
format. Parsing is done usingstrptime(3)
.
Therefore, you should be using:
date -f '%d/%m/%Y' +'%Y-%m-%d' ...
However, the residual issue is converting multiple values; you'd need multiple invocations of date
for that, so you'd like to try something like:
# Bogus code
while read date # Bogus code
do date -f '%d/%m/%Y' $date +'%Y-%m-%d' # Bogus code
done < tempDate # Bogus code
# Bogus code
This, however, attempts to set the system clock (not very successfully unless you're root
at the time, and you should not be root
when experimenting). So, there isn't an obvious way to get the date
command to do what you want.
Therefore, the answers using awk
, sed
, perl
, python
and regular expressions to map the dates are the way to go.
Upvotes: 2
Reputation: 2599
You can use a quick awk script to change to the year/month/day order:
awk -F '/' '{print $3"/"$2"/"$1}' < file.txt
Upvotes: 4
Reputation: 263307
The international standard date format, ISO 8601, is YYYY-MM-DD
, not YYYY/MM/DD
. I suggest using that unless you have a very good reason not to.
The US and UK date formats are ambiguous for dates before the 13th of each month, so it's not surprising that date
wouldn't recognize it. There could be a way to tell it that a given string is in UK format, but I don't know what it is.
You can use sed
, or any other tool that does regular expression substitution, to change the format:
$ echo 14/09/1992 | sed 's/\([0-9][0-9]\)\/\([0-9][0-9]\)\/\([0-9][0-9][0-9][0-9]\)/\3-\2-\1/'
1992-09-14
Perl can do this a bit less verbosely:
$ echo 14/09/1992 | perl -pe 's/(\d\d)\/(\d\d)\/(\d\d\d\d)/$3-$2-$1/'
1992-09-14
or:
$ echo 14/09/1992 | perl -pe 's/(\d{2})\/(\d{2})\/(\d{4})/$3-$2-$1/'
1992-09-14
Upvotes: 1