To get max/min Date/Timestamp from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To get max/min Date/Timestamp from a file
# 1  
Old 06-15-2011
To get max/min Date/Timestamp from a file

I want to get maximum/minimum date/timestamp from a data file ?

Sample Input File
=============
Code:
rec#,order_dt,ext_ts
1,2010-12-01,2010-12-01 17:55:23.222222
2,2011-11-05,2010-12-01 19:55:23.222222
3,2009-10-01,2010-12-01 18:55:23.222222


for above file
Code:
Maximum Order_dt = 2011-11-05
Minimum Order_dt = 2009-10-01
Maximum Ext_ts = 2010-12-01 19:55:23.222222
Minimum Ext_ts = 2010-12-01 17:55:23.222222

Can somebody help me on this ?

Last edited by Franklin52; 06-15-2011 at 03:40 AM.. Reason: Please use code tags
# 2  
Old 06-15-2011
Give us sample data, three rows isn't enough to test.
# 3  
Old 06-15-2011
Quote:
Originally Posted by kumaran_5555
Give us sample data, three rows isn't enough to test.
ok.

sample.txt attached.


Maximum(LOGONDATE) 2011-06-15
Minimum(LOGONDATE) 2011-05-31
Maximum(EXT_TS) 2011-06-15 07:41:14
Minimum(EXT_TS) 2011-05-31 10:16:49
# 4  
Old 06-15-2011
This code should work for you

If your seperator is pipe instead of comma use replace the dot with pipe inside -F"[-,:. ]"

Code:
awk -F"[-,:. ]" '

BEGIN{
min_date="99999999"
min_ext="99999999999999999999"

}
{
date=$2$3$4; ext=$5$6$7$8$9$10$11; 
if(min_date>date){min_date=date}; 
if(max_date<date){max_date=date};
if(min_ext>ext){min_ext=ext}
if(max_ext<ext){max_ext=ext}
}


END{

print "min date "min_date
print "max date " max_date
print "min ext "min_ext
print "max ext "max_ext
}' test.txt

Your sample file isn't matching the initial sample given here. may be few changes has to be done.

Last edited by kumaran_5555; 06-15-2011 at 04:11 AM..
This User Gave Thanks to kumaran_5555 For This Post:
# 5  
Old 06-15-2011
Yes its working.
Thanks a lot Smilie
# 6  
Old 06-15-2011
This should do what you want using the sample.txt file as attached to your post.

Code:
awk -F'|' '

    BEGIN {
        # Get the heading line out of the way
        getline;
        h2=$2;
        h3=$3;
        # Then prime min/max LOGONDATE/EXT_TS
        getline;
        min_2=$2;
        max_2=$2;
        min_3=$3;
        max_3=$3;

        while (getline) {
            # for all other lines apart from heading and first, check if they are min/max
            if (min_2>$2) min_2=$2;
            if (max_2<$2) max_2=$2;
            if (min_3>$3) min_3=$3;
            if (max_3<$3) max_3=$3;
        }

        # at end of input display
        printf "%-20s = %-30s\n" ,"min_"h2 ,min_2
        printf "%-20s = %-30s\n" ,"max_"h2 ,max_2
        printf "%-20s = %-30s\n" ,"min_"h3 ,min_3
        printf "%-20s = %-30s\n" ,"max_"h3 ,max_3
    }
' sample.txt


Last edited by Franklin52; 06-15-2011 at 09:53 AM.. Reason: Please use code tags, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find min and max time taken from a log file

You have a log file as attached in sample input with various operations and time taken by each of them. Write a script to find the min and max time taken for each operation. Sample output is attached. Sample Input is given as below: operation1,83621 operation2,72321 operation3,13288... (1 Reply)
Discussion started by: Chandan_Bose
1 Replies

2. Shell Programming and Scripting

Get min and max value in column

Gents, I have a big file file like this. 5100010002 5100010004 5100010006 5100010008 5100010010 5100010012 5102010002 5102010004 5102010006 5102010008 5102010010 5102010012 The file is sorted and I would like to find the min and max value, taking in the consideration key1... (3 Replies)
Discussion started by: jiam912
3 Replies

3. Shell Programming and Scripting

Average, min and max in file with header, using awk

Hi, I have a file which looks like this: FID IID MISS_PHENO N_MISS N_GENO F_MISS 12AB43131 12AB43131 N 17774 906341 0.01961 65HJ87451 65HJ87451 N 10149 906341 0.0112 43JJ21345 43JJ21345 N 2826 906341 0.003118I would... (11 Replies)
Discussion started by: kayakj
11 Replies

4. Shell Programming and Scripting

Print min and max value from two column

Dear All, I have data like this, input: 1254 10125 1254 10126 1254 10127 1254 10128 1254 10129 1255 10130 1255 10131 1255 10132 1255 10133 1256 10134 1256 10135 1256 10137... (3 Replies)
Discussion started by: aksin
3 Replies

5. Homework & Coursework Questions

Min/Max/counter/while loop from file

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: The program is supposed to read in text from a given file ( different samples provided in the homework but not... (1 Reply)
Discussion started by: c++newb
1 Replies

6. Shell Programming and Scripting

Converting a date to friday date and finding Min/Max date

Dear all, I have 2 questions. I have a file with many rows which has date of the format YYYYMMDD. 1. I need to change the date to that weeks friday date(Ex: 20120716(monday) to 20120720). Satuday/Sunday has to be changed to next week friday date too. 2. After converting the date to... (10 Replies)
Discussion started by: 2001.arun
10 Replies

7. Shell Programming and Scripting

Check if a date field has date or timestamp or date&timestamp

Hi, In a field, I should receive the date with time stamp in a particular field. But sometimes the vendor sends just the date or the timestamp or correctl the date&timestamp. I have to figure out the the data is a date or time stamp or date&timestamp. If it is date then append "<space>00:00:00"... (1 Reply)
Discussion started by: machomaddy
1 Replies

8. Shell Programming and Scripting

get min, max and average value

hi! i have a file like the attachement. I'd like to get for each line the min, max and average values. (there is 255 values for each line) how can i get that ? i try this, is it right? BEGIN {FS = ","; OFS = ";";max=0;min=0;moy=0;total=0;freq=890} $0 !~ /Trace1:/ { ... (1 Reply)
Discussion started by: riderman
1 Replies

9. Shell Programming and Scripting

how to find min, max dates in a file

hello friends...:-) i need some help i have a file cantain like this Star1 ,NetWork,09/02/2008 Star1 ,NetWork,10/02/2008 Star1 ,NetWork,11/02/2008 Star2 ,NetWork,08/03/2008 Star2 ,NetWork,09/04/2008 Star2 ,NetWork,10/05/2008 i need to find out min, max dates the output look like... (6 Replies)
Discussion started by: gemini106
6 Replies

10. Shell Programming and Scripting

min and max value of process id

We are running a AIX 5.2 OS. Would anyone happen to know what the max value for a process id could be? Thanks jerardfjay :) (0 Replies)
Discussion started by: jerardfjay
0 Replies
Login or Register to Ask a Question