parsing a file of datestamp


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting parsing a file of datestamp
# 1  
Old 03-11-2011
parsing a file of datestamp

i have a file which consists of the timestamp fields in the format

first packet: Sat Mar 5 16:14:33.678747 2011
first packet: Sat Mar 5 16:14:34.035362 2011
first packet: Sat Mar 5 16:14:39.805442 2011
first packet: Sat Mar 5 16:14:43.934197 2011
first packet: Sat Mar 5 16:14:43.935570 2011
first packet: Sat Mar 5 16:14:44.133906 2011
first packet: Sat Mar 5 16:14:54.171493 2011
first packet: Sat Mar 5 16:14:54.558706 2011
first packet: Sat Mar 5 16:14:55.508611 2011
first packet: Sat Mar 5 16:14:58.324469 2011
first packet: Sat Mar 5 16:14:58.328416 2011
first packet: Sat Mar 5 16:15:01.678607 2011
first packet: Sat Mar 5 16:15:01.680594 2011
first packet: Sat Mar 5 16:15:02.645661 2011
first packet: Sat Mar 5 16:15:02.650641 2011

I want to convert the data stored in this file in the format of date in java as
YYYY-MM-DD HH:mm:SS.sss
can i do this through a shell script ?
# 2  
Old 03-11-2011
Try this:
Code:
awk 'BEGIN {split("Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec",m,",")}
{ 
  for(i=1;i<13;i++) {
    if($4 == m[i]) { $4=sprintf("%02d", i) }
  }
  printf("%s %s %s-%s-%02d %s\n", $1, $2, $NF, $4, $5, $6)
}' file

Output:
Code:
$ cat file
first packet: Sat Mar 5 16:14:33.678747 2011
first packet: Sat Mar 5 16:14:34.035362 2011
first packet: Sat Mar 5 16:14:39.805442 2011
first packet: Sat Mar 5 16:14:43.934197 2011
first packet: Sat Mar 5 16:14:43.935570 2011
first packet: Sat Mar 5 16:14:44.133906 2011
first packet: Sat Mar 5 16:14:54.171493 2011
first packet: Sat Mar 5 16:14:54.558706 2011
first packet: Sat Mar 5 16:14:55.508611 2011
first packet: Sat Mar 5 16:14:58.324469 2011
first packet: Sat Mar 5 16:14:58.328416 2011
first packet: Sat Mar 5 16:15:01.678607 2011
first packet: Sat Mar 5 16:15:01.680594 2011
first packet: Sat Mar 5 16:15:02.645661 2011
first packet: Sat Mar 5 16:15:02.650641 2011
$ awk 'BEGIN {split("Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec",m,",")}
{ 
  for(i=1;i<13;i++) {
    if($4 == m[i]) { $4=sprintf("%02d", i) }
  }
  printf("%s %s %s-%s-%02d %s\n", $1, $2, $NF, $4, $5, $6)
}' file
first packet: 2011-03-05 16:14:33.678747
first packet: 2011-03-05 16:14:34.035362
first packet: 2011-03-05 16:14:39.805442
first packet: 2011-03-05 16:14:43.934197
first packet: 2011-03-05 16:14:43.935570
first packet: 2011-03-05 16:14:44.133906
first packet: 2011-03-05 16:14:54.171493
first packet: 2011-03-05 16:14:54.558706
first packet: 2011-03-05 16:14:55.508611
first packet: 2011-03-05 16:14:58.324469
first packet: 2011-03-05 16:14:58.328416
first packet: 2011-03-05 16:15:01.678607
first packet: 2011-03-05 16:15:01.680594
first packet: 2011-03-05 16:15:02.645661
first packet: 2011-03-05 16:15:02.650641
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. UNIX for Dummies Questions & Answers

Comparing two files with datestamp to current date

Hi, I am new to unix and I am stuck on how to compare two .zip file with date stamp in my directory. I need to compare out of the two file which is oldest to current date and unzip it after that done continue to unzip the second zip file. Thanks for your help. (5 Replies)
Discussion started by: lilvi3tboix1
5 Replies

3. Shell Programming and Scripting

move datestamp to beginning of line where available

My current output is as follows: All Day Event Someone's Birthday Class 7:00 PM Pick up dry cleaning 1:00 PM Wake up 8:00 AM I'd like the output to remain the same but have the times moved to the beginning of the line with a hyphen after it so it would look like, All Day Event... (3 Replies)
Discussion started by: chrish
3 Replies

4. Shell Programming and Scripting

Parsing of file for Report Generation (String parsing and splitting)

Hey guys, I have this file generated by me... i want to create some HTML output from it. The problem is that i am really confused about how do I go about reading the file. The file is in the following format: TID1 Name1 ATime=xx AResult=yyy AExpected=yyy BTime=xx BResult=yyy... (8 Replies)
Discussion started by: umar.shaikh
8 Replies

5. Shell Programming and Scripting

search files and copy them to a directory with datestamp attacched to it

Hi I need to search the some ftp files created in last 24 hours and copy them to a directory with date stamp attached to it. Iam using following command to search the files find $CA_OUT_PATH/*/ftp_out -type f -mtime -1 but now how to copy these files to some other directory one by one ... (1 Reply)
Discussion started by: sreenusola
1 Replies

6. UNIX for Dummies Questions & Answers

search files and copy them to a directory with datestamp attached to it

Hi I need to search the some ftp files created in last 24 hours and copy them to a directory with date stamp attached to it. Iam using following command to search the files find $CA_OUT_PATH/*/ftp_out -type f -mtime -1 but now how to copy these files to some other directory one by one with... (1 Reply)
Discussion started by: sreenusola
1 Replies

7. UNIX for Dummies Questions & Answers

unsure if this is a unix datestamp??? can you guys tell?

hi everyone im new here and im hoping you could help me out here. Yesterday i removed a time stamp from a program that i am using.This timestamp should contain a date of 04/04/2008 with a time sometime in that day. the stamp in hex is 7C FE 2B 04 DA 4E E3 40 ive looked everywhere and im... (1 Reply)
Discussion started by: uselessprog
1 Replies

8. Shell Programming and Scripting

simple help in datestamp

i have a problem here TZ=`date +%Z`+0 ;a=`date +%b%e`----gives me Feb27 but i want Feb 27.there should be a single space b/w Feb and 27.... can anyone tell me how to resolve this issues........ (1 Reply)
Discussion started by: ali560045
1 Replies

9. Shell Programming and Scripting

Substring/Instring of a string with datestamp

Hi, I have just started working on unix today. I want to get the instring of a string (filename). Eg. JAN_BILS_PRINT_01-01-08.txt Now i want to extract the datestamp from the file and convert the date to the format mm/dd/yyyy. How do i do this? Please hel pme with this. Regards,... (10 Replies)
Discussion started by: bhalotias
10 Replies

10. UNIX for Dummies Questions & Answers

cp files that have a particular datestamp

Hello all I'm trying to copy all files in the current directory with yesterday's date to another directory using a single command . I searched the faqs but all the examples listed out involve only the find command.I tried to do something like but it does'nt seem to work. cp < ls -l |grep... (2 Replies)
Discussion started by: luft
2 Replies
Login or Register to Ask a Question