Format/Fix Timestamp Data in a File.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Format/Fix Timestamp Data in a File.
# 1  
Old 01-07-2011
Java Format/Fix Timestamp Data in a File.

Hello Experts,

I have a timestamp(6) column in a .csv data file , format of the data is as below:-
Code:
ETCT,P,Elec, Inc.,abc,11/5/2010 4:16:09.000000 PM,Y,Y,Y

I want the timestamp column to be properly formatted like
Code:
11/05/2010 04:16:09.000000 PM

Currently the "0" is missing with date/month/hours for 200K some records.

I tried using AWK on this file -- but could change the value collectively and also in a file.
Code:
var='11/5/2010 4:16:09.000000 PM'
 
echo $var| awk 'BEGIN {FS="/"} {if (length($1) < 2) {print 0$1"/"$2"/"$3} else {print $0}}'| awk 'BEGIN {FS="/"} {if (length($2) < 2) {print $1"/"0$2"/"$3} else {print $0}}'


&
Code:
echo $var|awk 'BEGIN {FS=" "} {print $2" "$3 }' |awk 'BEGIN {FS=":"} {if (length($1) < 2) {print 0$1":"$2":"$3} else {print $0}}'|awk 'BEGIN {FS=":"} {if (length($2) < 2) {print $1":"0$2":"$3} else {print $0}}'

Any help how can i change the data to using AWK/SED etc?

Last edited by Franklin52; 01-08-2011 at 10:18 AM.. Reason: Please use code tags
# 2  
Old 01-07-2011
parse the numbers and use printf %02d. you dont want to hard code a leading zero.
# 3  
Old 01-08-2011
Java

Thanks Frank,

Please can you guide me how to do that, i am not an expert in that...
Please can you give an example....
# 4  
Old 01-08-2011
parse the output like you already are. just change the print statements to printf. using this method will work in all situations.

here is a simple example
Code:
$ echo 4|awk '{printf("%02d\n",$1)}'
04

$ echo 14|awk '{printf("%02d\n",$1)}'
14

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Timestamp format in HP-UX

I am on HP-UX B.11.11 OS. My requirement is to display a list of directories (not files) and the last modified time format should be DD-MON-YYYY HH:MI:SS AM/PM. I am able to get the list using either ls -lF | grep / OR ls -ld -- */ but I am unable to set the time format as I want. The... (5 Replies)
Discussion started by: nickz2017
5 Replies

2. Shell Programming and Scripting

Identify lines with wrong format in a file and fix

Gurus, I have a data file which has a certain number of columns say 101. It has one description column which contains foreign characters and due to this some times, those special characters are translated to new line character and resulting in failing the process. I am using the following awk... (4 Replies)
Discussion started by: tumsri
4 Replies

3. Shell Programming and Scripting

Convert UNIX timestamp to readable format in the file

Hello I have a file : file1.txt with the below contents : 237176 test1 test2 1442149024 237138 test3 test4 1442121300 237171 test5 test7 1442112823 237145 test9 test10 1442109600 In the above file fourth field represents the timestamp in Unix format. I found a command which converts... (6 Replies)
Discussion started by: rahul2662
6 Replies

4. Shell Programming and Scripting

Script to generate Excel file or to SQL output data to Excel format/tabular format

Hi , i am generating some data by firing sql query with connecting to the database by my solaris box. The below one should be the header line of my excel ,here its coming in separate row. TO_CHAR(C. CURR_EMP_NO ---------- --------------- LST_NM... (6 Replies)
Discussion started by: dani1234
6 Replies

5. UNIX for Dummies Questions & Answers

Format the data file

hi all, i am new to unix forum,i want do some large data format work. pls help me to format the file. i have attched data file to be formated in that two set of line item is there . first set header & second set header to be come in to single header item same as like all the data also. ... (2 Replies)
Discussion started by: dhamu
2 Replies

6. UNIX for Dummies Questions & Answers

How to Open a data format file?

Hi, Am having a file. I checked that file format by the following command file filename Output is filename: data So the file is data format file Am trying to view that file so i have used some commands like cat,more so on but it showing the contents like compressed form(full of Symbols). How... (4 Replies)
Discussion started by: Adhi
4 Replies

7. Shell Programming and Scripting

Getting file timestamp in certain format

Hi all, I'm a Unix newbie and I need to get file timestamp in the following format: YYYYMMDD HH24:MM:SS example: 20120713 18:49:30 For start I've tried the following code, but I don't know how to display the year and even format the date: ls -l $filename | awk '{print $7 $6 $8}' My... (4 Replies)
Discussion started by: Braun
4 Replies

8. Shell Programming and Scripting

Fix timestamp with Sed or Awk

Hi I am dealing with the following string: Date: Thur, 13 March 2011 01:01:10 +0000 I asked for help in another topic that converted a similar string: Date: Thur, 13 March 2011 9:50 AM To a 24 hr standard. The problem is that it comes out as: Date: Thur, 13 March 2011 9:50:00 +0000... (4 Replies)
Discussion started by: duonut
4 Replies

9. UNIX for Advanced & Expert Users

Sed to format data in a file

Hi , i need help with formatting a file i am generating which is to be used in mainframe app so the file length has to be 80 for each rows. The file that m able to generate looks like this (consists of two rows only) E 1006756 1006756 Active T E 0551055 0551055 Active T I... (2 Replies)
Discussion started by: cnilashis
2 Replies

10. UNIX for Advanced & Expert Users

get the timestamp of a file in desired format

Hi, I have a file say abc. I get the timestamp in following way: ls -ltr abc | awk -F" " '{print $6,$7,$8}' Mar 8 10:23 I need to get the timestamp as : 03-08-2007 10:23:00 Thanks Sumeet (1 Reply)
Discussion started by: sumeet
1 Replies
Login or Register to Ask a Question