Convert date format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert date format
# 1  
Old 10-05-2015
Convert date format

Hi guys
I am looking to convert this kind of entry in a txt file
Code:
26/04/2008

to
Code:
April 2008

Note : this is not using the date command , these are date entries in a file

can i do this with sed ?

Moderator's Comments:
Mod Comment Use code tags, thanks.

Last edited by zaxxon; 10-05-2015 at 08:36 AM.. Reason: code tags
# 2  
Old 10-05-2015
Refer to this thread :


Convert Date Format
# 3  
Old 10-05-2015
Please use code tags for posting data, logs etc.
Also post 2-3 example lines of your data please, so that we can easier avoid pitfalls and provide a better solution.

Blind shot:

Code:
$ cat infile
03/02/2008 [AA] This is some data here.
26/04/2008 [BB] This is some data here.
$ sed '
s,/01/, January ,g
s,/02/, February ,g
s,/03/, March ,g
s,/04/, April ,g
' infile
03 February 2008 [AA] This is some data here.
26 April 2008 [BB] This is some data here.

This User Gave Thanks to zaxxon For This Post:
# 4  
Old 10-05-2015
apologies, this is what i have

Code:
cat $1 | awk -F, '{print  $1 "," $2}' | sed '1d' | sed 's/IE//' | sed 's/^\(.\{7\}\)/\1 /' | sed 's/^\(.\{9\}\)/\1 /' | sed 's/,/ /'  | awk '{ print $4 " " $1 " "  $2 " "  $3}'| sort -k4 -n

This is the output, the output is what i want but i want to get the dates look like "February 2008"

Code:
26/04/2008 1416658 6 0266
13/01/2009 1414244 9 0333
14/04/2010 1418044 4 0367
19/01/2007 1514244 2 0395
13/04/2010 1617482 6 0472
27/04/2010 1516657 7 0597
04/02/2010 1612892 4 0602
02/02/2008 1516760 5 0636
25/11/2009 1510244 6 0850
30/03/2010 1414235 5 1639
21/02/2009 1510106 1 2687

I am sure there are other better ways of improving my current script but it does what i want it to do fine, so please just help me with the date bit

---------- Post updated at 07:05 AM ---------- Previous update was at 07:03 AM ----------

Quote:
Originally Posted by zaxxon
Please use code tags for posting data, logs etc.
Also post 2-3 example lines of your data please, so that we can easier avoid pitfalls and provide a better solution.

Blind shot:

Code:
$ cat infile
03/02/2008 [AA] This is some data here.
26/04/2008 [BB] This is some data here.
$ sed '
s,/01/, January ,g
s,/02/, February ,g
s,/03/, March ,g
s,/04/, April ,g
' infile
03 February 2008 [AA] This is some data here.
26 April 2008 [BB] This is some data here.

this looks fine , how can i incorporate this to what i have ?

Last edited by zaxxon; 10-05-2015 at 09:35 AM..
# 5  
Old 10-05-2015
Post the relevant input file to these 11 lines of example in code tags please.
There is a lot of duplicate calls of the tools so we can maybe put it all into 1 more compact command.
The initial cat is not needed at all.
awk and sed as well as most other tools that take input from files can open them on their own without it.
# 6  
Old 10-05-2015
Hi.

See also a package of date utilities, among them dconv for conversion, as in this demonstration:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate date conversion of dates in file, dconv.
# Package dateutils in some Linux repositories, e.g. Debian 
# otherwise, see: http://www.fresse.org/dateutils/ or:
# https://github.com/hroptatyr/dateutils

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C dconv

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Results:"
# e.g. 26/04/2008 to April 2008
dconv -S -i '%d/%m/%Y' -f '%B %Y' < $FILE

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian 5.0.8 (lenny, workstation) 
bash GNU bash 3.2.39
dconv 0.2.6

-----
 Input data file data1:
26/04/2008 1416658 6 0266
13/01/2009 1414244 9 0333
14/04/2010 1418044 4 0367
19/01/2007 1514244 2 0395
13/04/2010 1617482 6 0472
27/04/2010 1516657 7 0597
04/02/2010 1612892 4 0602
02/02/2008 1516760 5 0636
25/11/2009 1510244 6 0850
30/03/2010 1414235 5 1639
21/02/2009 1510106 1 2687

-----
 Results:
April 2008 1416658 6 0266
January 2009 1414244 9 0333
April 2010 1418044 4 0367
January 2007 1514244 2 0395
April 2010 1617482 6 0472
April 2010 1516657 7 0597
February 2010 1612892 4 0602
February 2008 1516760 5 0636
November 2009 1510244 6 0850
March 2010 1414235 5 1639
February 2009 1510106 1 2687

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Date Format

Hello, I want to change the format of date value in variable. e.g. cur_date = '2013/03/13 14:24:50' (yyyy/mm/dd hh24:mi:ss) I want to change this to '13-MAR-2013 14:24:50 Following code coverts the current date to format I am looking for. But I do not know how this can be done for a date... (8 Replies)
Discussion started by: cartrider
8 Replies

2. Shell Programming and Scripting

Convert string into date format

Hi, I am getting the below string as a input for date. 12/03/2013 11:02 AM I want to change this date as 03-DEC-2013 11:02 AM. Could you please help on this. Thanks Chelladurai (4 Replies)
Discussion started by: ckchelladurai
4 Replies

3. Shell Programming and Scripting

awk convert date format

Could you tell me how to convert the following dates? If I have m/d/yyyy, I want to have 0m/0d/yyyy. I want my dates to always be 8 digits. In other words, I want a 0 inserted whenever the month or day is a single digit. My issue is first I need to use FS="," to get field $4 for the... (7 Replies)
Discussion started by: wbrunc
7 Replies

4. Shell Programming and Scripting

Convert any date format into yyyy/mm/dd

How can I convert any user inputted date into yyyy/mm/dd ? For example user can input date one of the following 20120121 , 2012-01-21 ,01/21/2012,01/21/2012 etc But I need to convert any of the date entered by user into yyyy/mm/dd (2012/01/2012). Any suggestion. Thanks in advance this is... (1 Reply)
Discussion started by: ZeroHedge
1 Replies

5. Shell Programming and Scripting

convert date format

I've been using this thread: https://www.unix.com/shell-programming-scripting/58675-change-date-dd-mmm-yyyy-mm-dd-yyyy.html and https://www.unix.com/shell-programming-scripting/14655-changing-yyyy-mm-dd-ddmmyy.html and this code: on this format: 05/16/2008 18:30:49 Installation 48985and I'm... (3 Replies)
Discussion started by: dba_frog
3 Replies

6. Shell Programming and Scripting

Convert date to Unix format

Dear Expert How to convert date in format of YYYY-MM-DD HH:MM:SS to unix format using a script or command if avaliable Example "2011-05-15 18:00:00" is converted to 1330970400 I tried to use option d in date command but no use, Im using solaris 10 Thanks a lot (12 Replies)
Discussion started by: yahyaaa
12 Replies

7. Shell Programming and Scripting

convert date format to mysql date format in log file

I have a comma delimited log file which has the date as MM/DD/YY in the 2nd column, and HH:MM:SS in the 3rd column. I need to change the date format to YYYY-MM-DD and merge it with the the time HH:MM:SS. How will I got about this? Sample input 02/27/09,23:52:31 02/27/09,23:52:52... (3 Replies)
Discussion started by: hazno
3 Replies

8. Shell Programming and Scripting

Convert Epoch time format to normal date time format in the same file

I have a file named "suspected" with series of line like these : {'protocol': 17, 'service': 'BitTorrent KRPC', 'server': '219.78.120.166', 'client_port': 52044, 'client': '10.64.68.44', 'server_port': 8291, 'time': 1226506312L, 'serverhostname': ''} {'protocol': 17, 'service': 'BitTorrent... (3 Replies)
Discussion started by: rk4k
3 Replies

9. UNIX for Dummies Questions & Answers

Convert date format

Hi All, Need your help in converting a date format in ksh. I'm currently working on SUN os where my script is getting a date from a table. The result returns to ksh in this format: 17-JUL-08 How do i convert this string to a date format like yyyymmdd? I tried #!/bin/ksh d="17-JUL-08"... (5 Replies)
Discussion started by: agathaeleanor
5 Replies

10. Shell Programming and Scripting

convert mmddyy date format to ccyyddd format??

hi, for reading a cobol indexed file i need to convert "mmddyy" date format to "ccyyddd" format. i checked the datecalc and other scripts but couldnt modify them to cater to my need:(... The datecalc gives an output which i believe is the total days till that date, but i want to convert it... (2 Replies)
Discussion started by: Bhups
2 Replies
Login or Register to Ask a Question