Change the date and time format in UNIX script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change the date and time format in UNIX script.
# 8  
Old 06-07-2014
If I understand what your input file looks like (working backwards from your code), the following awk script seems to do what you want:
Code:
#!/bin/ksh
INPUTFILE=${1:-data}
tdat=$(awk -F'|' '
BEGIN {	M[1] = "JAN"
	M[2] = "FEB"
	M[3] = "MAR"
	M[4] = "APR"
	M[5] = "MAY"
	M[6] = "JUN"
	M[7] = "JUL"
	M[8] = "AUG"
	M[9] = "SEP"
	M[10] = "OCT"
	M[11] = "NOV"
	M[12] = "DEC"
}
{	# Convert date in field 9 from "MM/DD/YYYY" to "DD-MMM-YY".
	d = substr($9, 4, 2) "-" M[substr($9, 1, 2) + 0] "-" substr($9, 9, 2)
	# Convert time in field 10 from "HH:MM:SS" to "hh.MM.SS.000000000 xM"
	H = substr($10, 1, 2)
	m = substr($10, 4, 2)
	S = substr($10, 7, 2)
	AP = H >= 12 ? "P" : "A"
	h = H + 0 > 12 ? H - 12 : H + 0 == 0 ? 12 : H
	printf("%s %02d.%s.%s.000000000 %sM\n", d, h, m, S, AP)
	exit
}' "$INPUTFILE"
)
printf "1st line from file \"%s\" is " "$INPUTFILE"
head -1 "$INPUTFILE"
printf "tdat has been set to \"%s\"\n" "$tdat"

I normally use the Korn shell, but this script will work with any shell that accepts basic POSIX shell requirements for command substitution and parameter expansions (such as bash and ksh). If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.

You can invoke this script with the name of your input data file (it defaults to using a file named data if you don't supply an operand). If you have a data file that contains:
Code:
f1|f2|f3|f4|f5|f6|f7|f8|06/05/2014|16:04:00junk|f11|f12
f1|f2|f3|f4|f5|f6|f7|f8|01/31/2013|00:01:02|f11|f12
f1|f2|f3|f4|f5|f6|f7|f8|02/29/2012|03:04:05|f11|f12
f1|f2|f3|f4|f5|f6|f7|f8|03/30/2011|06:07:08|f11|f12
f1|f2|f3|f4|f5|f6|f7|f8|04/28/2010|09:10:11|f11|f12
f1|f2|f3|f4|f5|f6|f7|f8|05/27/2010|12:13:14|f11|f12
f1|f2|f3|f4|f5|f6|f7|f8|06/26/2009|15:16:17|f11|f12
f1|f2|f3|f4|f5|f6|f7|f8|07/25/2008|18:19:20|f11|f12
f1|f2|f3|f4|f5|f6|f7|f8|08/24/2007|21:22:23|f11|f12
f1|f2|f3|f4|f5|f6|f7|f8|09/23/2006|01:02:03|f11|f12
f1|f2|f3|f4|f5|f6|f7|f8|10/22/2005|04:05:06|f11|f12
f1|f2|f3|f4|f5|f6|f7|f8|11/21/2004|07:08:09|f11|f12
f1|f2|f3|f4|f5|f6|f7|f8|12/20/2003|10:11:12|f11|f12

it produces the output:
Code:
1st line from file "data" is f1|f2|f3|f4|f5|f6|f7|f8|06/05/2014|16:04:00junk|f11|f12
tdat has been set to "05-JUN-14 04.04.00.000000000 PM"

To verify that the month translations are all working correctly, if you remove (or comment out) the exit in the awk script, it will produce the output:
Code:
tdat has been set to "05-JUN-14 04.04.00.000000000 PM
31-JAN-13 12.01.02.000000000 AM
29-FEB-12 03.04.05.000000000 AM
30-MAR-11 06.07.08.000000000 AM
28-APR-10 09.10.11.000000000 AM
27-MAY-10 12.13.14.000000000 PM
26-JUN-09 03.16.17.000000000 PM
25-JUL-08 06.19.20.000000000 PM
24-AUG-07 09.22.23.000000000 PM
23-SEP-06 01.02.03.000000000 AM
22-OCT-05 04.05.06.000000000 AM
21-NOV-04 07.08.09.000000000 AM
20-DEC-03 10.11.12.000000000 AM"

# 9  
Old 06-08-2014
Thank You SOOOOO much Don!! You have saved my life!! Thank you very very much! It is working perfectly well!Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change date format in am/pm in csv files using UNIX

Hi All, I'm new to forum good to hear all. I stuck in converting date format in csv file using unix csv file contains as below ,750,0000000000000000GCJR, ,06/22/2016 14:48:44 I want to convert into as below ,750,0000000000000000GCJR, ,06/22/2016 02:48:44 PM Please reply asap..... (22 Replies)
Discussion started by: Raghureds
22 Replies

2. Shell Programming and Scripting

Script to change date/time

Hi everybody! I need to perform a task with a script but I have no idea how to do it, I hope someone could help me: - on my linux pc I have many folders with movies, tv shows, toons, ecc. They are shared by a dlna server to my panasonic tv where I can browse and see them. The problem is that... (6 Replies)
Discussion started by: Torquemada
6 Replies

3. Shell Programming and Scripting

Date format change in UNIX .dat file

Hi, I need help to convert the date format in .DAT file in unix. I want to convert 10@@|SWIFT MT568 Extract@@|Apr 14 2014 5:47:52:563PM@@|Apr 14 2014 4:33:47:663PM@@||##| into 10@@|SWIFT MT568 Extract@@|04/14/2014/ 5:47:52:563PM@@|04/14/2014 4:33:47:663PM@@||##| Appreciate... (18 Replies)
Discussion started by: karthikengox
18 Replies

4. UNIX for Dummies Questions & Answers

Rename all Files in a UNIX Directory from one date format to another date format

Hi Unix Gurus, I would like to rename several files in a Unix Directory . The filenames can have more than 1 underscore ( _ ) and the last underscore is always followed by a date in the format mmddyyyy. The Extension of the files can be .txt or .pdf or .xls etc and is case insensitive ie... (1 Reply)
Discussion started by: pchegoor
1 Replies

5. Shell Programming and Scripting

Change date format in shell script

Plz help me To display date in the mm/dd/yyyy. Eg. if date is 28-09-2012 the output of the shell script should be date 09/28/2012. (1 Reply)
Discussion started by: shivasaini
1 Replies

6. UNIX for Dummies Questions & Answers

Shell Scripts - shows today’s date and time in a better format than ‘date’ (Uses positional paramete

Hello, I am trying to show today's date and time in a better format than ‘date' (Using positional parameters). I found a command mktime and am wondering if this is the best command to use or will this also show me the time elapse since 1/30/70? Any help would be greatly appreciated, Thanks... (3 Replies)
Discussion started by: citizencro
3 Replies

7. OS X (Apple)

how to change date and time format in menu bar

Can someone please tell me how to change the time and date format in the menu bar. I get to System Preferences -- Language and Text, format, customize, but I can't figure out what to do next. I want to change, say from Tue May 4 to 5 4 2010. How the heck do I do this? (5 Replies)
Discussion started by: Straitsfan
5 Replies

8. Shell Programming and Scripting

help for change date format script

Someone can help me write a script for change date fromat from "Feb 4 18:44:03 2009" to 2009020418 ? from "Mar 17 16:44:03 2009" to 2009031716 ? (4 Replies)
Discussion started by: pccwtest
4 Replies

9. 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

10. Shell Programming and Scripting

script to change the date format in a file

i have many files with date format of 6-9-2008 and i want a script that can change the format to 2008-06-09 Thanks (15 Replies)
Discussion started by: shehzad_m
15 Replies
Login or Register to Ask a Question