Extract date from files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract date from files
# 1  
Old 10-02-2014
Extract date from files

Hi Guys,

I am having a file in below format:

Code:
job1|start|01/01/1988 10:10:10
job2|start|01/01/1988 10:10:10
job1|start|01/01/1988 11:10:10
job1|end|01/01/1988 12:10:10
job2|end|01/01/1988 12:10:10
job3|start|01/01/1988 14:10:10
job4|end|01/01/1988 15:10:10

I need the output like this:

Code:
job1|start|01/01/1988 11:10:10|end|01/01/1988 12:10:10
job2|start|01/01/1988 10:10:10|end|01/01/1988 12:10:10
job3|start|01/01/1988 14:10:10
job4|end|01/01/1988 15:10:10

If the job has start it should consider the latest start time my file will have the latest end time only.

If the job is having only start time or end tim , then display start time in the format above like how job3 and job4 is having

I have tried something like this but couldn't able to achieve :

Code:
awk '{i=$1; $1=x; A[i]=A[i] $0} $2=="end"{print i A[i]}' FS=\| OFS=\| file

# 2  
Old 10-02-2014
Perhaps this:

Code:
awk '
/start/{s[$1]="start|"$3}
/end/{e[$1]="end|"$3;s[$1]=s[$1]}
END{for(k in s) print k,s[k](length(s[k])&&(k in e)?"|":x)e[k] }' FS=\| OFS=\| file

# 3  
Old 10-03-2014
Code:
$ cat file
job1|start|01/01/1988 10:10:10
job2|start|01/01/1988 10:10:10
job1|start|01/01/1988 11:10:10
job1|end|01/01/1988 12:10:10
job2|end|01/01/1988 12:10:10
job3|start|01/01/1988 14:10:10
job4|end|01/01/1988 15:10:10

Code:
$ awk -F\| '{A[$1] = /start/ ? $0 : $1 in A && /end/  ? A[$1] FS $2 FS $3 : $0 }END{for(i in A)print A[i]}' file


Resulting

Code:
job1|start|01/01/1988 11:10:10|end|01/01/1988 12:10:10
job2|start|01/01/1988 10:10:10|end|01/01/1988 12:10:10
job3|start|01/01/1988 14:10:10
job4|end|01/01/1988 15:10:10

This User Gave Thanks to Akshay Hegde For This Post:
# 4  
Old 10-03-2014
Thanks Akshay for your snippet which really worked
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract date from files based on file pattern

I want to extract dates from the files and i have different types of files with pattern. I have list file with the patterns and want to date extract based on it in a sh script Files in the directory : file1_20160101.txt file2_20160101_abc.txt filexyz20160101.txt list file with... (2 Replies)
Discussion started by: lijjumathew
2 Replies

2. Shell Programming and Scripting

Extract count of string in all files and display on date wise

Hi All, hope you all are doing well! I kindly ask you for shell scripting help, here is the description: I have huge number of files shown below on date wise, which contains different strings(numbers you can say) including 505001 and 602001. ... (14 Replies)
Discussion started by: VasuKukkapalli
14 Replies

3. Shell Programming and Scripting

How to list files that are not first two files date & last file date for every month-year?

Hi All, I need to find all files other than first two files dates & last file date for month and month/year wise list. lets say there are following files in directory Mar 19 2012 c.txt Mar 19 2012 cc.txt Mar 21 2012 d.txt Mar 22 2012 f.txt Mar 24 2012 h.txt Mar 25 2012 w.txt Feb 12... (2 Replies)
Discussion started by: Makarand Dodmis
2 Replies

4. Shell Programming and Scripting

Extract date / time

How do i display in the below format without the brackets using shell script. Tue Oct 1 13:32:40 2013 Please use CODE tags not only for all code segments, input samples, and output samples. (7 Replies)
Discussion started by: ghosh_tanmoy
7 Replies

5. Shell Programming and Scripting

Extract week start,end date from given date in PERL

Hi All, what i want to do in perl is i should give the date at run time .Suppose date given is 23/12/2011(mm/dd/yyyy) the perl script shold find week start date, week end date, previous week start date,end date,next week start date, end date. In this case week start date will be-:12/19/2011... (2 Replies)
Discussion started by: parthmittal2007
2 Replies

6. Shell Programming and Scripting

Sorting Files by date and moving files in date order

I need to build a k shell script that will sort files in a directory where files appear like this "XXXX_2008021213.DAT. I need to sort by date in the filename and then move files by individual date to a working folder. concatenate the files in the working folder then start a process once... (2 Replies)
Discussion started by: rebel64
2 Replies

7. Shell Programming and Scripting

extract date

I run few command from c shell script and after this i want to check the status. I write the following code and it give the error message like "Variable Syntax". Here is my code: #/ !/bin/csh -f rm -f <filename> if ($? != 0) then echo " command does not executed successfully!" end... (3 Replies)
Discussion started by: skumar11
3 Replies
Login or Register to Ask a Question