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
# 8  
Old 06-30-2016
yes If I used direct in unix its working fine
if I used same stmts in shell script its giving AM/22/2016 14:48:44
# 9  
Old 06-30-2016
Quote:
Originally Posted by Raghureds
yes If I used direct in unix its working fine
if I used same stmts in shell script its giving AM/22/2016 14:48:44
well... maybe the problem is in the script?
# 10  
Old 07-01-2016
i'm using same in shell script I placed $variable in place of 06/22/2016 14:48:44

---------- Post updated at 01:09 AM ---------- Previous update was at 12:00 AM ----------

Code:
echo ',750,0000000000000000GCJR, ,$date_value' | \
   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'

im using above stmt but its not working any idea please

---------- Post updated at 01:53 AM ---------- Previous update was at 01:09 AM ----------

in shell script im assigning value to ldr_dt vaiable then passing as below

Code:
echo $lrd_dt|\
   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'

Out put : PM22/2016 02:48:44

but I need output like this 06/22/2016 02:48:44 PM



Moderator's Comments:
Mod Comment Please use code tags as required by forum rules!

Last edited by RudiC; 07-01-2016 at 04:15 AM.. Reason: Added code tags.
# 11  
Old 07-01-2016
Might be a DOS (<CR>, \r, 0x0D) line terminator? Please post a hexdump of the result.

What exactly is the contents of the lrd_dt variable?
# 12  
Old 07-01-2016
lrd_dt contains 06/22/2016 14:48:44

Last edited by RudiC; 07-01-2016 at 04:30 AM..
# 13  
Old 07-01-2016
Code:
echo "$lrd_dt" |\

# 14  
Old 07-01-2016
Code:
echo $lrd_dt
06/22/2016 02:48:44

Code:
echo $lrd_dt | od -c
0000000    0   6   /   2   2   /   2   0   1   6       0   2   :   4   8
0000020    :   4   4  \r  \n
0000025

Code:
$ echo $lrd_dt|   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'
 AM22/2016 02:48:44

Code:
echo $lrd_dt|   awk '{
      sub(/\r/, "")
      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'

Code:
06/22/2016 02:48:44 AM

or

Code:
echo $$lrd_dt|   awk '{
      split($NF,a,":"); pm=int(a[1]/12)
      if (pm) a[1]=sprintf("%02d",a[1]%12)
      $NF=a[1]":"a[2]":"sub(/\r/, "", a[3]) OFS ((pm)?"PM":"AM")
   }1'

Code:
06/22/2016 02:48:1 AM


Last edited by Aia; 07-01-2016 at 11:46 AM..
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