Need to convert 12h to 24h time in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to convert 12h to 24h time in file
# 1  
Old 08-20-2008
Need to convert 12h to 24h time in file

I have a file that is exported from a database, I have no control on the raw output from the source system. The file is comma delimited with a header row.

The second field in the file is a time field, from the source system it exports in 12h time in the following format. "h:mm:ss AM" or "hh:mm:ss AM" (of course the AM can also be PM).

So, the problem. I need this file sorted by date and time, the date column is easy, the time column is a problem. The sort command seems to only group all the AM and PM times together, so my thinking was that using a 24 hour time format would do the trick.

I should warn you this is my first shell script, so I may be missing something obvious, but there seems to be a lack of date and time functions in script

Specifics:

This is on an appliance - Redhat ES 4 - most shell stuff is installed, GCC is not.

Shell is bash


The file format....

Code:
Date,Time,Data1,Data2,Data3
M/DD/YYYY,H:MM:SS AM,text string,text string,number
MM/DD/YYYY,HH:MM:SS PM,text string,text string,number

and so on.

I did find the following thread, which gets me close, but I need to put it together with reading and writing from a file.

https://www.unix.com/shell-programmin...onversion.html

Thanks in advance
# 2  
Old 08-20-2008
awk -F, '{gsub(/\//," ",$1); gsub(/:/," ",$2)};{print $1, $2, $3, $4, $5}' file | sort -k1,3n -k7,7 -k4,6n | awk '{printf("%s/%s/%s,%s:%s:%s %s,%s,%s,%s,%s\n", $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11);}'

Work on this. You should get idea...only some formatting..
# 3  
Old 08-21-2008
Quote:
Originally Posted by sudhamacs
awk -F, '{gsub(/\//," ",$1); gsub(/:/," ",$2)};{print $1, $2, $3, $4, $5}' file | sort -k1,3n -k7,7 -k4,6n | awk '{printf("%s/%s/%s,%s:%s:%s %s,%s,%s,%s,%s\n", $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11);}'

Work on this. You should get idea...only some formatting..
Thanks for the reply. I get what you are doing here and it almost works. It sorts by date and time correctly, however....

In the original post, where my header row indicates "data1, data2, data3," etc.
The third and fourth field in the original comma delimited format (data1 and data2) are variable length text fields with spaces, data3 is numeric. The awk script above puts commas in the spaces in the text fields.

In addition to that, the text string in field four (data2) is one of four values, so a specific date/time combination shows up four times in each file. This means I will also need a sort on field four to keep them grouped.

If I'm thinking clearly, I need to sort on data2, date and time.
# 4  
Old 08-21-2008
awk -F, '{gsub(/\//,"|",$1); gsub(/:/,"|",$2); gsub(/ /,"|",$2);};{print $1,"|",$2,"|",$3,",",$4,",",$5}' file| sort -t"|" -k1,3n -k7,7 -k4,6n | awk -F"|" '{gsub(/\|/,"/",$1); printf("%s/%s/%s,%s:%s:%s %s,%s\n", $1,$2,$3,$4,$5,$6,$7,$8);}'
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert UTC time into current UNIX sever time zone

Hi guys thanks for the help for my previous posts.Now i have a requirement that i download a XMl file which has UTC time stamp.I need to convert UTC time into Unix server timezone. For ex if the time zone of unix server is CDT then i need to convert into CDT.whatever may be the system time... (5 Replies)
Discussion started by: mohanalakshmi
5 Replies

2. Shell Programming and Scripting

Shell script to convert epoch time to real time

Dear experts, I have an epoch time input file such as : - 1302451209564 1302483698948 1302485231072 1302490805383 1302519244700 1302492787481 1302505299145 1302506557022 1302532112140 1302501033105 1302511536485 1302512669550 I need the epoch time above to be converted into real... (4 Replies)
Discussion started by: aismann
4 Replies

3. Shell Programming and Scripting

Find and Convert UTC Time to PST Time

Hello All - I have a script that grabs data from the net and outputs the following data 46029 46.144 -124.510 2010 07 26 22 50 320 4.0 6.0 2.2 9 6.8 311 1012.1 -0.9 13.3 13.5 13.3 - - 46041 47.353 -124.731 2010 07 26 22 50 250 2.0 3.0 1.6 8 6.4 - 1011.6 - ... (0 Replies)
Discussion started by: drexnefex
0 Replies

4. AIX

Convert UTC time to local time ?

Hello, Using AIX6.1 box. I have UTC time value and need to convert it to local time value - I mean time zone and DST should be taken into consideration. I hope it could be done using shell environment - I don't want to write a program. thanks Vilius ---------- Post updated at 02:30 PM... (2 Replies)
Discussion started by: vilius
2 Replies

5. Shell Programming and Scripting

how to convert date time to epoch time in solaris

Hi, Is there any easy way to convert date time(stored in shell variable ) to epoch time in solaris box? As +%s is working on linux but not on solaris, also -d option is not working. Any suggestion please? (6 Replies)
Discussion started by: anshuman0507
6 Replies

6. Shell Programming and Scripting

Convert Unix Time to Standard Time

I have a list of interfaces and time the interface was last active. I can't figure out how to convert the time in the second column, Fa1/14 0 Se0/0/0 0 Fa1/11 0 Fa1/9 0 Fa1/0 0 Se0/0/1 1240401408 Gi1/0 0 Fa0/0 1240401408 Fa1/3 0 Fa1/8 0 Fa1/15 0 Fa1/13 0 Fa1/10 0 Fa1/1 0 Fa1/12... (7 Replies)
Discussion started by: mrlayance
7 Replies

7. UNIX for Advanced & Expert Users

Convert to time format

I Have variable $currenttime and I want to display this variable as a TIME FORMAT and the $currenttime value contains like 000000 120000 020000 I want to display to 00:00:00 12:00:00 02:00:00 please help me how to this one (1 Reply)
Discussion started by: gksenthilkumar
1 Replies

8. Shell Programming and Scripting

Convert Epoch Time to Standard Date and Time & Vice Versa

Hi guys, I know that this topic has been discuss numerous times, and I have search the net and this forum for it. However, non able to address the problem I faced so far. I am on Solaris Platform and unable to install additional packages like the GNU date and gawk to make use of their... (5 Replies)
Discussion started by: DrivesMeCrazy
5 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. AIX

time convert

Hi Friends, I see the last login time as time_last_login=1210762918 How to convert this to standard format. I believe there is a command, I am not able to recollect it. Thanks in advance (10 Replies)
Discussion started by: b_manu78
10 Replies
Login or Register to Ask a Question