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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change date format in am/pm in csv files using UNIX
# 1  
Old 06-30-2016
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
Code:
,750,0000000000000000GCJR, ,06/22/2016 14:48:44

I want to convert into as below
Code:
,750,0000000000000000GCJR, ,06/22/2016 02:48:44 PM

Please reply asap.. thanks in advance


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules, not multiple FONT tags

Last edited by RudiC; 07-01-2016 at 04:12 AM.. Reason: Changed FONT tags to CODE tags.
# 2  
Old 06-30-2016
Code:
date -d "06/22/2016 14:48:44" '+%m/%d/%Y %r'

# 3  
Old 06-30-2016
Thanks Pravin for response.

I'm getting -d not found error

could you please post to convert file at a time command

---------- Post updated at 07:43 AM ---------- Previous update was at 07:42 AM ----------

this is the error " ksh: -d: not found"
# 4  
Old 06-30-2016
Code:
echo ",750,0000000000000000GCJR, ,06/22/2016 11:48:44"  | awk -F"," '{
hr=substr($NF,12,2);
if (hr > 11) { b="PM"
hr-12 == 0 ? nhr=hr : nhr=hr-12
} else { b="AM";nhr=hr}
printf "%s%02d%s %s\n",substr($NF,1,11),nhr,substr($NF,14),b}'

I guess your date version is not GNU. Could you please try above code
# 5  
Old 06-30-2016
a different take:
Code:
 echo ',750,0000000000000000GCJR, ,06/22/2016 14:48:44' | \
   awk '{
      split($NF,a,":"); pm=int(a[1]/12)
      if (pm) a[1]=sprintf("%02d",a[1]%12)
      $NF=a[1]":"a[2]":"a[3] OFS ((pm)?"PM":"AM")
   }1'


Last edited by vgersh99; 06-30-2016 at 10:36 AM.. Reason: pm fix
# 6  
Old 06-30-2016
Thanks for both its working fine but when I used this in my shell script its adding at beging like AM/22/2016 14:48:44
# 7  
Old 06-30-2016
Quote:
Originally Posted by Raghureds
Thanks for both its working fine but when I used this in my shell script its adding at beging like AM/22/2016 14:48:44
strange - I get the desired output with the sample line you posted.
Could you post a sample not-working line again, please.
And also what OS are you on?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Date format change in a csv file

Hi, We have csv file where date is coming in MM/DD/YYYY HH:MM:SS (06/23/2015 20:59:12) in multiple places But we need to change the date format to DD/Mon/YYYY HH:MM:SS (23/Jul/2015 20:59:12) using shell script. Please let us know how can we achieve the same. (16 Replies)
Discussion started by: dholea
16 Replies

2. Shell Programming and Scripting

Need to change date format in a csv file using awk

Example: Input csv file 00245DLS,Sitel Ocala,12/31/2014,18:45,1.00,7.00,0.00,0.00 00245DLS,Sitel Ocala,12/31/2014,19:00,-1.00,-1.00,-1.00,-1.00 00245HB,Charlotte,01/01/2015,00:00,-1.00,-1.00,-1.00,0.00 Output csv file 00245DLS,Sitel Ocala,2014/12/31,18:45,1.00,7.00,0.00,0.00 00245DLS,Sitel... (8 Replies)
Discussion started by: adit
8 Replies

3. Shell Programming and Scripting

Datestamp format 2nd change in csv file (awk or sed)

I have a csv file formatted like this: 2014-08-21 18:06:26,A,B,12345,123,C,1232,26/08/14 18:07and I'm trying to change it to MM/DD/YYYY HH:MM for both occurances. I have got this: awk -F, 'NR <=1 {print;next}{"date +%d/%m/%Y\" \"%H:%m -d\""$1 "\""| getline dte;$1=dte}1' OFS="," test.csvThis... (6 Replies)
Discussion started by: say170
6 Replies

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

5. Shell Programming and Scripting

Change the date and time format in UNIX script.

Hi, I am extracting a date string from the source file like this : 06/05/2014 16:04:00 I want to change it to 05-JUN-14 04.05.00.000000000 PM I basically store the date in a variable. I got solutions to change date in dd-mmm-yyyy format using tr but I guess it works only with the "date"... (8 Replies)
Discussion started by: Varshha
8 Replies

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

7. Shell Programming and Scripting

Changing date format in CSV file

I have a CSV file with a date format like this; 11/19/2012 17:37:00,1.372,121.6 11/19/2012 17:38:00,0.743,121.6 Want to change the time stamp to seconds after 1970 so I can get the data in rrdtool. For anyone interested, this is data from a TED5000 unit and is Kwatts and volts. Needs to... (3 Replies)
Discussion started by: ottsm
3 Replies

8. Shell Programming and Scripting

Retaining the Unix CSV format in Excel format while exporting

Hi All, I have created a Unix Shell script whch creates a *.csv file and export it to Excel. The problem i am facing is that Users wants one of the AMOUNT field in comma separted values. Example : if the Amount has the value as 3000000 User wants to be in 3,000,000 format. This Amount format... (2 Replies)
Discussion started by: rawat_me01
2 Replies

9. Shell Programming and Scripting

Format a date in a csv file

So I have a csv file where the 3rd field is a date string in the format yyyy-mm-dd. I need to change it to mm/dd/yyyy. So each line in the csv file looks like: StringData,StringData,2009-02-17,12.345,StringData StringData,StringData,2009-02-16,65.789,StringData Any idea how I can keep... (5 Replies)
Discussion started by: rpiller
5 Replies

10. Shell Programming and Scripting

How to Change the Format of a Date

Hi All, this is my second post, last post reply was very helpful. I have a data that has date in DD/MM/YYYY (07/11/2008) format i want to replace the backslash by a dot(.) so that my awk script can read it inside the C shell script that i have written. i want to change 07/11/2008 to... (3 Replies)
Discussion started by: asirohi
3 Replies
Login or Register to Ask a Question