Sponsored Content
Full Discussion: DATE TIME formatting
Homework and Emergencies Emergency UNIX and Linux Support DATE TIME formatting Post 302757031 by RudiC on Thursday 17th of January 2013 03:11:58 AM
Old 01-17-2013
This is not only not easy, but some records will be impossible to translate, be it manually or by program logic - those using two numbers below 13 for day and month in an undefined order. These will be printed to an error file. For the rest, and if your awk versions allows for it, try:
Code:
$ awk  'BEGIN {split("Jan Feb Mar Apr May Jun Jul AUg Sep Oct Nov Dec", months)}
         NR==1 {print;next}
         {n=split ($1,Dt,"[-/ :]")                                            # split all date/time info into up to 7 array elements
          if (!(Dt[1]+0))                     {Mon=Dt[1]; Day=Dt[2]}          # check if there's an alpha month's name; use it, the other element is the day
            else if (!(Dt[2]+0))              {Mon=Dt[2]; Day=Dt[1]}
                   else if (Dt[1]>12)         {Mon=months[Dt[2]]; Day=Dt[1]}  # check if there's one element greater than 12 - must be a day; use it, the other is a month
                          else if (Dt[2]>12)  {Mon=months[Dt[1]]; Day=Dt[2]}
                                 else         {print > "errorfile";next}      # all other records cannot be correctly translated

          if (Dt[7]=="PM") Dt[4]+=12                                          # handling PM may need to be refined
         
          print Day"-"Mon"-"Dt[3], Dt[4]":"Dt[5]":" sprintf ("%02d",Dt[6]+0), $2, $3, $4,$5
         }
         
        ' FS="," file
DATE,PR,ST,ONC,NOD
19-Jul-1979 18:00:00 748 1 43 1
25-Jan-1979 06:00:00 748 1 43 1
20-Jan-1979 06:00:00 748 1 43 1
31-Sep-1979 06:00:00 748 1 43 1
20-Jan-1979 06:00:00 748 1 43 1
31-Sep-1979 06:00:00 748 1 43 1
25-Jul-1979 03:00:00 748 1 43 1

The AM/PM logics may need some refinement, as does the output of the time fields in general. Regard this as an exercise for yourself...

Last edited by RudiC; 01-17-2013 at 04:24 AM..
This User Gave Thanks to RudiC For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Formatting date

i need date in the following format December 14, 2005. With date +"%b %d, %Y" command i am getting the following output :- Dec 14, 2005. can anyone pls tell me how to get the full month name (2 Replies)
Discussion started by: radhika03
2 Replies

2. Shell Programming and Scripting

date formatting

Date format MM/DD/YYYY required is YYYYMMDD, I tried using sed but could not get it any help please. (4 Replies)
Discussion started by: mgirinath
4 Replies

3. UNIX for Dummies Questions & Answers

Date formatting

Running bash how do I input the date in the command line like 3/20/90 and get an output formmated like March, 20 1990. (8 Replies)
Discussion started by: knc9233
8 Replies

4. Shell Programming and Scripting

date formatting

Hi i need to have the date in the format like dd-mon-yyyy my script goes like this #!/usr/bin/bash for f in /space/can /home/lbs/current/externalcdrbackup/L_CDR_Configuration/1/200903122* ; do awk '{sum++;}END{for(i in sum) {print d,h,m,i, sum}}' "d=$(date +'%m-%d-%Y')" "h=$(date +'%H')"... (8 Replies)
Discussion started by: aemunathan
8 Replies

5. OS X (Apple)

Date Formatting, etc.

Hi - I'm using GeekTool to customize my desktop in OS X 10.5.8 I'm a complete novice as far as UNIX commands, just know enough to be dangerous. I have a command entered as a Shell to display my events from iCal: This makes my events show something like this: While this is... (1 Reply)
Discussion started by: patricksprague
1 Replies

6. Shell Programming and Scripting

Formatting a date

Hi, the date value retrieved by a parameter from the table is of the format dd/mm/yyyy. please let me know how to convert this to YYYYMMDD using sed thanks (4 Replies)
Discussion started by: swasid
4 Replies

7. Shell Programming and Scripting

Formatting date

Hi all Y=`date +'%Y'` M=`date +'%m'` D=`date +'%d'` if && ;then yesterday=$Y$M`expr $D + 30` echo $yesterday else if && ; then yesterday=$Y$M`expr $D + 29` echo $yesterday else if ; then yesterday=$Y$M`expr $D + 27` echo $yesterday else yesterday=$Y$M`expr $D - 1` echo... (8 Replies)
Discussion started by: ultimatix
8 Replies

8. Shell Programming and Scripting

Formatting date time in unix

while read l do vTimeCreated=`perl -e '@d=localtime ((stat(shift))); printf "%02d-%02d-%04d %02d:% 02d:%02d\n", $d,$d+1,$d+1900,$d,$d,$d' ${l}` echo "${l} || ${vTimeCreated}" >> ${fPrefx}_Output_Files_${vDate}.txt done < servername.txt Using the above code to format date time for each of the... (0 Replies)
Discussion started by: HeadBang
0 Replies

9. UNIX for Dummies Questions & Answers

Formatting date time in unix using perl breaks

while read l do vTimeCreated=`perl -e '@d=localtime ((stat(shift))); printf "%02d-%02d-%04d %02d:% 02d:%02d\n", $d,$d+1,$d+1900,$d,$d,$d' ${l}` echo "${l} || ${vTimeCreated}" >> ${fPrefx}_Output_Files_${vDate}.txt done < servername.txt Using the above code to format date time for each of... (5 Replies)
Discussion started by: HeadBang
5 Replies

10. UNIX for Dummies Questions & Answers

Need Date Formatting help

Hi, How can i store the date + time from the output of the ls command in loop in a variable date1? -rw-rw---- 1 user1 admin 500002 Jan 2 21:24 P002607.cssI then want to convert Jan 2 21:24 to this date format 2014-01-02 21:24:00 and save it in date2 variable. Then i would like to add... (1 Reply)
Discussion started by: mohtashims
1 Replies
holidays(4)							   File Formats 						       holidays(4)

NAME
holidays - prime/nonprime table for the accounting system SYNOPSIS
/etc/acct/holidays DESCRIPTION
The /etc/acct/holidays file describes which hours are considered prime time and which days are holidays. Holidays and weekends are con- sidered non-prime time hours. /etc/acct/holidays is used by the accounting system. All lines beginning with an "*" are comments. The /etc/acct/holidays file consists of two sections. The first non-comment line defines the current year and the start time of prime and non-prime time hours, in the form: current_year prime_start non_prime_start The remaining non-comment lines define the holidays in the form: month/day company_holiday Of these two fields, only the month/day is actually used by the accounting system programs. The /etc/acct/holidays file must be updated each year. EXAMPLES
Example 1: Example of the /etc/acct/holidays file. The following is an example of the /etc/acct/holidays file: * Prime/Nonprime Table for the accounting system * * Curr Prime Non-Prime * Year Start Start * 1991 0830 1800 * * only the first column (month/day) is significant. * * month/day Company Holiday * 1/1 New Years Day 5/30 Memorial Day 7/4 Indep. Day 9/5 Labor Day 11/24 Thanksgiving Day 11/25 day after Thanksgiving 12/25 Christmas 12/26 day after Christmas SEE ALSO
acct(1M) SunOS 5.10 28 Mar 1991 holidays(4)
All times are GMT -4. The time now is 06:54 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy