Calculate N days from a file output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calculate N days from a file output
# 1  
Old 07-20-2010
Calculate N days from a file output

OK, here is the output from a cron I have here:

FULL OUTPUT:
Code:
acoxxx Lastlogin= 2010/07/15 13:10
db2t Lastlogin= 2010/07/16 13:09
db2tadm Lastlogin= 2010/07/20 13:09
eisuser Lastlogin= 2010/07/20 11:53
israel Lastlogin= 2010/07/10 11:42
nmon Lastlogin= 2010/07/05 12:55
norbac Lastlogin= 2010/07/04 14:21
oracle Lastlogin= 2010/07/03 08:05
root Lastlogin= 2010/07/03 13:01
s03josa0 Lastlogin= 2010/07/01 14:10

I'm looking for a script that 'grep' ONLY the lines older than N days.
Example: "Lastlogin older than 10 days":
OUPUT:
Code:
nmon Lastlogin= 2010/07/05 12:55
norbac Lastlogin= 2010/07/04 14:21
oracle Lastlogin= 2010/07/03 08:05
root Lastlogin= 2010/07/03 13:01
s03josa0 Lastlogin= 2010/07/01 14:10

How can I do this?

thanks
ISrael.

Moderator's Comments:
Mod Comment [QUOTE] if you quote someone or something. [CODE] for code and listings, please.

Last edited by pludi; 07-20-2010 at 10:36 AM..
# 2  
Old 07-20-2010
lets just say your cron output is stored in a file named file1

then you can use something like this:-

Code:
 awk -v f=0 '/2010/07/10/ {f=0} { print > "file_"f }'  file1

this will place all the lines before the date 2010/07/10 into file0. but in this case you have to enter the date manually. i mean the date 10days earlier date. or you can take it as a parameter. or pass it via giving output of find command.
# 3  
Old 07-20-2010
Hi daz,

it gives me an error:

Code:
israel@bsades2: /home/israel # awk -v f=0 '/2010/07/10/ {f=0} { print > "file_"f }' /tmp/prueba1
 Syntax Error The source line is 1.
 The error context is
                /2010/07/10/ >>>  { <<<
 awk: 0602-500 Quitting The source line is 1.

What's wrong? Thanks

---------- Post updated at 09:44 AM ---------- Previous update was at 09:17 AM ----------

daz,

What if I the date is 36 days?

thanks
Israel.

Last edited by iga3725; 07-20-2010 at 11:42 AM..
# 4  
Old 07-20-2010
With GNU date, or you give the date directly to $DATE by format as 20100710

Code:
DATE=$(date -d "36 days ago" +%Y%m%d)
awk -v d="$DATE" '{t=$3;gsub(/\//,"",t)}{if (t<d) print} ' urfile

# 5  
Old 07-21-2010
Hi rdcwayx,

I think I don't have gnu-date because I'm on AIX6.1. Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Subtract 2 date columns in .csv file and get output as number of days

Hi, I have one .csv file. I have 2 date columns present in file, column 2 and column 3. I need to calculate how many days exist between 2 dates. I am trying to subtract date column 2 from date column 3. Eg: my file look likes s.no, Start_date,End_Date 1, 7/29/2012,10/27/2012 2,... (9 Replies)
Discussion started by: Dimple
9 Replies

2. Shell Programming and Scripting

Calculate the number of days between 2 dates - bash script

I wrote the day calculator also in bash. I would like to now, that is it good so? #!/bin/bash datum1=`date -d "1991/1/1" "+%s"` datum2=`date "+%s"` diff=$(($datum2-$datum1)) days=$(($diff/(60*60*24))) echo $days Thanks in advance for your help! (3 Replies)
Discussion started by: kovacsakos
3 Replies

3. Shell Programming and Scripting

KSH script Not working (calculate days since 1/1/2000 given day 4444)

I am unable to get this KSH script to work. Can someone help. I've been told this should work with KSH93. Which I think I have on Solaris 10. If I do a grep -i version /usr/dt/bin/dtksh I get @(#)Version M-12/28/93d @(#)Version 12/28/93 @(#)Version M-12/28/93 This is correct for... (5 Replies)
Discussion started by: thibodc
5 Replies

4. Web Development

Calculate the number of days between 2 dates - PHP

Is this code good for this purpose? <?php $date1 = mktime(0,0,0,01,01,1991); $date2 = mktime(0,0,0,03,22,2012); $diff = $date2 - $date1; $days = $diff / (60*60*24); echo ($days . "<br />"); ?> (3 Replies)
Discussion started by: kovacsakos
3 Replies

5. Shell Programming and Scripting

Calculate days between yyyyMmmdd dates on Solaris

I extract dates from the log file and need to calculate days between two dates. My dates are in yyyyMmmdd format. Example: $d1=2011 Oct 21 $d2=2012 Feb 20 I need to calculate the number of days between $d2 and $d1. This is on Solaris. Any ideas? Thanks, djanu (4 Replies)
Discussion started by: djanu
4 Replies

6. Shell Programming and Scripting

Calculate age of a file | calculate time difference

Hello, I'm trying to create a shell script (#!/bin/sh) which should tell me the age of a file in minutes... I have a process, which delivers me all 15 minutes a new file and I want to have a monitoring script, which sends me an email, if the present file is older than 20 minutes. To do... (10 Replies)
Discussion started by: worm
10 Replies

7. Shell Programming and Scripting

Calculate 30/31 days from today date script

Hi Guys, I was working some time ago n was in need to calculate date 30/31 days from today including Feb (Leap yr stuff). Today date is variable depending on day of execution of script. I tried searching but was not able to get exactly what I needed....So at that I time I implemented by my own... (3 Replies)
Discussion started by: coolgoose85
3 Replies

8. Shell Programming and Scripting

Script to calculate user's last login to check if > 90 days

I need a script to figure out if a user's last login was 90 days or older. OS=AIX 5.3, shell=Korn Here's what I have so far: ==== #!/usr/bin/ksh NOW=`lsuser -a time_last_login root | awk -F= '{ print $2 }'` (( LAST_LOGIN_TIME = 0 )) (( DIFF = $NOW - $LAST_LOGIN_TIME )) lsuser -a... (3 Replies)
Discussion started by: pdtak
3 Replies

9. Shell Programming and Scripting

calculate the number of days left in a month

does any one have any ideas how i would go about calculating the number of days left in the month from a bash script ?. I want to do some operations on a csv file according to the result (8 Replies)
Discussion started by: dunryc
8 Replies

10. Shell Programming and Scripting

calculate output

I was wondering can anyone give me a clue how to start script which would do the following: I have 2 numbers as input for example: 100 and 1000 and I need to create file and in that file should be written 100 - 199 200 - 299 300 - 399 400 - 499 500 - 599 600 - 699 700 - 799... (3 Replies)
Discussion started by: amon
3 Replies
Login or Register to Ask a Question