duration calculation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting duration calculation
# 1  
Old 04-13-2010
duration calculation

I have a file which has 3 coloumns emp_name, Joining_date, Designation.

Code:
abc	12/1/2001	SSE
def	2/25/2007	SE
ghi	3/18/2009	SA
abc	8/1/2008	SSE
def	2/13/2007	SE
ghi	3/24/2005	SA

I need to find out the emp who has been in the company for longest period(Till date).

Can I have any suggestion, how to calculate the duration till date?
Thanks
# 2  
Old 04-13-2010
convert the dates from the 'MM/DD/YYYY' to 'YYYYMMDD' format and get the lowest number. The rest should be easy.
# 3  
Old 04-13-2010
if you are using Linux, below is my generic script for calculating # of days different between 2 given dates. using a -today flag as many calcs have today's date as one of the inputs. It should accept about any UNIX date format, at least all I've tried.

Code:
#!/usr/bin/bash

if [ $# -ne "2" ]; then
        echo "incorrect format:   $0 date date"
        echo "example: ./list_times 01/16/2009 04/01/2009"
        echo "example: ./list_times 2009-01-16 04/01/2009"
        exit 1
fi

if [ $2 = "-today" ]; then
        TODAY=`date +%m/%d/%Y`
        /usr/bin/echo -n  $1 " "

                 echo "("$(date -d $1 +%s)-$(date -d $TODAY +%s)")/86400" | bc
 else
                 echo "("$(date -d $1 +%s)-$(date -d $2 +%s)")/86400" | bc
fi


Code:
# for your example, I simply created a file called emp, and run it through a for loop, but should give you an idea how to incorporate into a script.

for EMP in `awk '{ print $2 }' emp`
  do 
      ./list_times $EMP -today
done

output:
8/1/2008 -620
3/24/2005 -1846
3/18/2009 -391
2/25/2007 -1142
2/13/2007 -1154
12/1/2001 -3054

Last edited by denn; 04-13-2010 at 02:35 PM..
# 4  
Old 04-13-2010
Tools Here is an approach to get you started

Assuming the data is in empl.txt

Code:
>cat empl.txt | tr "/" " " | awk '{printf "%04d%02d%02d,%-40s,%-40s\n", $4,$2,$3,$1,$5}' | sort
20011201,abc                                     ,SSE
20050324,ghi                                     ,SA
20070213,def                                     ,SE
20070225,def                                     ,SE
20080801,abc                                     ,SSE
20090318,ghi                                     ,SA

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Duration Calculation

I have 2 variables startTime='122717 23:20' endTime='122817 0:40' how can i get the elapsed duration as like "1 hour 20 minutes" ? (8 Replies)
Discussion started by: vikram3.r
8 Replies

2. Shell Programming and Scripting

Need help in Inix script for finding duration

I have a file with time in it. I need to find the duration between the timestamp by subtracting second row from third row and so on. and wherever it is more than 30 minutes it should display start and end time which have been subtracted file : 00:44:11 00:44:11 00:44:13 00:44:13 00:46:51... (2 Replies)
Discussion started by: Muskaan
2 Replies

3. UNIX for Beginners Questions & Answers

Process duration

Hi , How can I check that for a single process, for example pagent for how much duration this process was up or down and also I need multiple entries if this process was down or up multiple times. Please help. (3 Replies)
Discussion started by: Ashish Garg
3 Replies

4. UNIX for Dummies Questions & Answers

How to list files for particular duration of time .?

Hi there is a lot of file dated from last week till ofpresent date. if i want to list only last 3 days files using ls command how can i do it please suggest. below is the list of the file. and i want to list files from MAy 12 to May 16. Nov 22 2011 NSSM.UPPLSCPLB81 Jan 12... (2 Replies)
Discussion started by: scriptor
2 Replies

5. Shell Programming and Scripting

Sort by Duration

.......................................................................................................................... 03:40 Geonetric File from CCL Complete 03:40:59 03:41:08 00:00:09 00:00:01 N/A 005 sys_runccl ... (7 Replies)
Discussion started by: Daniel Gate
7 Replies

6. UNIX Desktop Questions & Answers

arecord not interrupted after specified duration

I have used the arecord command like this arecord -d 1 test.wav It is keep on waiting. I need to manually interrupt it by ctrl-c. Why it is not interrupting after one second? The arecord version which I am using is : arecord: version 1.0.23 by Jaroslav Kysela (3 Replies)
Discussion started by: thillai_selvan
3 Replies

7. Shell Programming and Scripting

Convert duration of the process to seconds

Hi, I am looking to write a script to kill the process which are running for more than 7 days. So i have a command like "ps -eo pid,etime,args | grep -i xxxx" ( process which has xxx in it and running for more than 7 days needs to be killed ). When i exeucte the above command , i am... (2 Replies)
Discussion started by: forums123456
2 Replies

8. UNIX for Dummies Questions & Answers

Copy duration of cp

Hello forum, i would like to ask if there's a way to view the remaining time of copying files (talking about copying gigabytes) while the cp commnad is running. I'm using OpenBSD 4.9 -stable. Thanx in advance. :) (2 Replies)
Discussion started by: sepuku
2 Replies

9. Solaris

ufsdump backup duration

hi, i'm trying to figure out how to tell the amount of time a ufsdump of a directory takes. i use the below command: echo "Starting Backup of u4" >> /backup/backup.log 2>&1 /usr/sbin/ufsdump 0uf /dev/rmt/0n /u4 >> /backup/backup.log 2>&1 echo "Finished Backup of u4" >> /backup/backup.log... (0 Replies)
Discussion started by: pinoy43v3r
0 Replies

10. Shell Programming and Scripting

date duration fail to calculate

Hi Everyone, I was very sad after a long way but still cannot figure out the duration between two date. $date1="20090812 23:48:56"; $date2="20090813 00:01:37"; The output will be "001241". I did the following tries, like print localtime(UnixDate(ParseDate("20090812 23:48:56"),"%s"));... (2 Replies)
Discussion started by: jimmy_y
2 Replies
Login or Register to Ask a Question