how to find min, max dates in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to find min, max dates in a file
# 1  
Old 03-14-2008
Question 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 this
=================
Star1 09/02/2008
Star1 11/02/2008

Star2 08/03/2008
Star2 10/05/2008

can anyone suggest me what i can do?
thnx in advance
# 2  
Old 03-14-2008
Bug 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 this
=================
Star1 09/02/2008
Star1 11/02/2008

Star2 08/03/2008
Star2 10/05/2008

can anyone suggest me what i can do?
thnx in advance
# 3  
Old 03-14-2008
input:
Code:
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

outpit
Code:
csadev:/home/jmcnama> cat newfile
Star1 ,NetWork,09/02/2008
Star1 ,NetWork,11/02/2008
Star2 ,NetWork,08/03/2008
Star2 ,NetWork,10/05/2008

The code assumes the input file is unsorted to start with, you can remove the sort if you want.
Code:
#!/bin/ksh
epoch_secs()
{
        perl -e '
                use POSIX qw(strftime);
                $fmt = "%s";  # %s = epoch seconds
                # mm/dd/yyyy
                $mday = substr("$ARGV[0]",3 , 2);
                $mon =  substr("$ARGV[0]", 0 ,2);
                $year = substr("$ARGV[0]", 6 ,4);
                $epoch_sec =
                  strftime($fmt, 0, 0, 0, $mday , $mon - 1, $year - 1900, -1, -1, -1);
                print "$epoch_sec";
                ' "$1"
}
while IFS=, && read fld1 fld2 fld3
do
	echo "$fld1,$fld2,$fld3,$(epoch_secs $fld3)"      	
done < filename  | sort -t"," -k1.1,1.6 -k4.1,4.10 | \
    awk -F, '{ if(NR==1) {
                     print sprintf("%s,%s,%s",$1, $2, $3) 
                     keep=$1; 
                     old=sprintf("%s,%s,%s",$1, $2, $3) 
              }
              if($1!=keep) { 
                       print old;                        
                       print sprintf("%s,%s,%s",$1, $2, $3) 
                       keep=$1
                       }
              old=sprintf("%s,%s,%s",$1, $2, $3 )
            }  
              END { print sprintf("%s,%s,%s",$1, $2, $3)  }              
           '  > newfile

# 4  
Old 03-14-2008
Python Solution

Here's something I did in Python. It works as desired, although it may be able to be simplified by someone better at coding Python. Hope it helps.

Code:
#!/usr/bin/env python

"""
   Read the log file, returning the highest and lowest dates for each star_num
"""



import time
import sys
import os

##########
#Make sure we have an input file.
##########
try:
    input_file = sys.argv[1]
except IndexError:
    sys.stderr.write("No input file specified.")
    sys.exit(1)

##########
#Make sure we can open the file
##########
try :
    input = open(input_file, 'r')
except IOError:
    sys.stderr.write("File is invalid.")
    sys.exit(1)
    
def compareDates(x, y, prefer="high"):

    """
        Return higher or lower of two dates
    """

    x_epoch = time.mktime(time.strptime(x, "%m/%d/%Y"))
    y_epoch = time.mktime(time.strptime(y, "%m/%d/%Y"))

    if prefer == "high":
        if x_epoch <= y_epoch:
            myPick = y
        else:
            myPick = x
    else:
        if x_epoch >= y_epoch:
            myPick = y
        else:
            myPick = x

    return myPick


tracker = {}

for line in input:

    ################
    #strip newline
    ################
    line = line.rstrip("\n")

    ################
    #get fields
    ################
    star_num, location, date = line.split(",")

    ################
    #dictionary holding current min and max values
    ################
    minMax = tracker.get(star_num, {})

    old_min = minMax.get("min", "")
    old_max = minMax.get("max", "")

    if old_min == "":
        minMax["min"] = date
    else:
        minMax["min"] = compareDates(date, minMax["min"], "low")

    if old_max == "":
        minMax["max"] = date
    else:
        minMax["max"] = compareDates(date, minMax["max"], "high")

    tracker[star_num] = minMax

input.close()


################
#sort star_nums
################
keys = tracker.keys()
keys.sort()

for key in keys:

    print key, tracker[key]["min"]
    print key, tracker[key]["max"]

# 5  
Old 03-15-2008
GnUawk
Code:
awk 'BEGIN{FS=","}
{
 n=split($3,dm,"/")
 y=dm[3];m=dm[2];d=dm[1]
 da=mktime(y" "m" "d" 0 0 0") 
 if ( da > date[$1]  ){
   date[$1]=da" "date[$1]
 }else {
   date[$1]=date[$1]" "da
 } 
 odate[da]=$3
}
END{
 for ( i in date) {
  m=split(date[i],temp," ")
  printf "max: %s, min: %s->%s\n" , odate[temp[1]],odate[temp[3]],i  
 }
}' file

output:
Code:
# ./test.sh
max: 11/02/2008, min: 09/02/2008->Star1
max: 10/05/2008, min: 08/03/2008->Star2


if you fancy Python solution
Code:
#!/usr/bin/env python
import time
d={};e={}
for n,l in enumerate(open("file")):    
    line=[i.strip() for i in l.strip().split(",")]
    thedate = [ int(k) for k in  line[2].split("/") ]
    t = time.mktime( (thedate[2],thedate[1],thedate[0],0,0,0,0,0,0) ) 
    d.setdefault(line[0],[])
    d[line[0]].append(t)
    e[t]=line[2]
for m,n in d.iteritems():
    s=sorted(n)
    print "Max: %s ,  Min: %s -> %s" %( e[s[2]],e[s[0]],m )

output:
Code:
 # ./test.py
Max: 11/02/2008 ,  Min: 09/02/2008 -> Star1
Max: 10/05/2008 ,  Min: 08/03/2008 -> Star2

# 6  
Old 03-16-2008
Another Awk solution:

Code:
awk 'END { for (k in min) 
printf "%s %s\n%s %s\n\n", 
k, min[k], k, max[k] }
!x[$1]++ { min[$1] = $3 }
fmtd($3) > fmtd(max[$1]) { max[$1] = $3 }
fmtd($3) < fmtd(min[$1]) { min[$1] = $3 } 
func fmtd(dt) { split(dt, t, "/") 
  return t[3]t[1]t[2] 
}' FS="," file

Use nawk or /usr/xpg4/bin/awk on Solaris.
# 7  
Old 03-17-2008
MySQL

hi...Smilie

Thanks a lot to ......Smilie
jim mcanamara
shawnMilo
ghostdog74
radoulov Smilie

its working fine....Smilie


regads,
gemini
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

awk script to find min and max value

I need to find the max/min of columns 1 and 2 of a 2 column file what contains the special character ">". I know that this will find the max value of column 1. awk 'BEGIN {max = 0} {if ($1>max) max=$1} END {print max}' input.file But what if I needed to ignore special characters in the... (3 Replies)
Discussion started by: ncwxpanther
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. 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

5. Shell Programming and Scripting

to find min and max value for each column!

Hello Experts, I have got a txt files which has multiple columns, I want to get the max, min and diff (max-min) for each column in the same txt file. Example: cat file.txt a 1 4 b 2 5 c 3 6 I want ouput like: cat file.txt a 1 4 b 2 5 c 3 6 Max 3 6 Min 1 4 Diff 2 2 awk 'min=="" ||... (4 Replies)
Discussion started by: dixits
4 Replies

6. Shell Programming and Scripting

How to find the average,min,max ,total count?

Hi , Below is my sample data,I have this 8 column(A,B,C,D,E,F,G,H) in csv file. A , B ,C ,D ,E ,F,G ,H 4141,127337,24,15,20,69,72.0,-3 4141,128864,24,15,20,65,66.0,-1 4141,910053,24,15,4,4,5.0,-1 4141,910383,24,15,22,3,4.0,-1 4141,496969,24,15,14,6,-24.0,-18... (7 Replies)
Discussion started by: vinothsekark
7 Replies

7. Shell Programming and Scripting

Find min.max value if matching columns found using AWK

Input_ File : 2 3 4 5 1 1 0 1 2 1 -1 1 2 1 3 1 3 1 4 1 6 5 6 6 6 6 6 7 6 7 6 8 5 8 6 7 Desired output : 2 3 4 5 -1 1 4 1 6 5 6 8 5 8 6 7 (3 Replies)
Discussion started by: vasanth.vadalur
3 Replies

8. Shell Programming and Scripting

To get max/min Date/Timestamp from a file

I want to get maximum/minimum date/timestamp from a data file ? Sample Input File ============= 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 Maximum Order_dt = 2011-11-05... (5 Replies)
Discussion started by: vikanna
5 Replies

9. UNIX for Dummies Questions & Answers

How to find whenther given value is in betwwen min and Max in unix shell scripting

Hi I wanted to write a shell script with an if condition Example MinValue=10 MaxValue=30 logvalue = some integer value that script reads from the command line arguement I wanted to check whether log value greater than or equal to10 and less than equal to 30 and proceed with the rest of... (5 Replies)
Discussion started by: pinky
5 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