![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| time convert | b_manu78 | AIX | 10 | 09-16-2008 10:47 AM |
| Convert milliseconds to standard time | chiru_h | Shell Programming and Scripting | 1 | 07-19-2007 10:45 AM |
| how to convert epoch time to readible format? | cin2000 | Shell Programming and Scripting | 11 | 12-19-2005 04:14 PM |
| Convert UTC time to Date | GNMIKE | Shell Programming and Scripting | 8 | 10-19-2005 11:43 PM |
| Convert from standard epoch time from a shell script? | LordJezo | Shell Programming and Scripting | 4 | 09-18-2005 08:48 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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 I did find the following thread, which gets me close, but I need to put it together with reading and writing from a file. Date conversion Thanks in advance |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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
|
|||
|
|||
|
Quote:
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
|
|||
|
|||
|
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);}'
|
|||
| Google The UNIX and Linux Forums |