[SOLVED] Pull lines older than N weeks


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [SOLVED] Pull lines older than N weeks
# 1  
Old 09-20-2012
[SOLVED] Pull lines older than N weeks

There is another post in the forums that is similar to what I am trying to do, however, the thread is closed. So, I am creating this new one to see if someone could help. I am trying to use the code Ahamed posted, and tweak it.

With the info from the forum, I recreated the scenario the person had, and used the last code snippet as a test to see what happened. It worked great. However, I am trying to tweak it for what I need, and was wondering if someone could help.

In my output/file I have something like this:
Code:
PROD_OFFSI  N00215    28 Jul 2010      28 Jul 2010
PROD_OFFSI  N00216    08 Aug 2012   08 Aug 2012
PROD_OFFSI  N00226    25 Jul 2012     25 Jul 2012
PROD_OFFSI  N00210    18 Nov 2009   18 Nov 2009
PROD_OFFSI  N00236    12 Sep 2012   12 Sep 2012
PROD_OFFSI  N00300    25 Aug 2010   25 Aug 2010
PROD_OFFSI  N00266    11 Aug 2010   11 Aug 2010
PROD_OFFSI  N00273    29 Apr 2010    29 Apr 2010
PROD_OFFSI  N00283    14 Apr 2010    14 Apr 2010
PROD_OFFSI  N00262    05 Sep 2012    05 Sep 2012
PROD_OFFSI  N00354    05 Sep 2012    05 Sep 2012
PROD_OFFSI  N00200    11 Jul 2012      11 Jul 2012

What I am trying to do is, pull the N00XXX row for anything over 5 weeks using the date in the last row. Not deleting any rows, need this for another function. The code looks like it converts dates to an epoch time format. Is that correct? I am able to do that using awk. EX: awk '{print "date -d\""$6FS$7FS$8"\" +%s"}' |bash

It converts the date fine, but when I try to do this in a shell script, it is a no go. In the code, it is set like this year=${val:6:4}; month=${val:3:2}; day=${val:0:2}
What are the numeric properties in this. I am guessing the second number is spacing length, like 4 is 4 spaces, and what is the first number?


Any help on this would be appreciated. Thanks!


Code from thread:

Code:
#!/bin/bash
  
ref=$( date '+%s' )
while read line
do
  val=${line#*,}; val=${val%%,*}
  year=${val:6:4}; month=${val:3:2}; day=${val:0:2}
  sec=$( date -d"$year/$month/$day" +%s )
  ((diff=(ref-sec)/(60*60*24)))
  test "$diff" -le 30 && echo $line
done < infile


Last edited by Corona688; 09-20-2012 at 06:26 PM..
# 2  
Old 09-20-2012
Quote:
Originally Posted by karstenjhilton
What I am trying to do is, pull the N00XXX row for anything over 5 weeks using the date in the last row. Not deleting any rows, need this for another function. The code looks like it converts dates to an epoch time format. Is that correct? I am able to do that using awk. EX: awk '{print "date -d\""$6FS$7FS$8"\" +%s"}' |bash
That's not just awk, that's awk, date, and shell.

What system do you have? In Linux, awk has the ability to do some date math natively.
# 3  
Old 09-20-2012
Hey Corona688,
Sorry, my head is mush from working on this all day. Should have put Nix version, and etc.

AIX 6.3 is the OS. Using bash instead of ksh, but not opposed to ksh.

Definitely a mix of stuff for this script. Smilie

Thanks for replying.
# 4  
Old 09-20-2012
You do not have date -d on AIX so that's not going to work. That's a GNU date thing.
# 5  
Old 09-20-2012
I am able to use the date -d. I have used it before. Here is a quick convert on command line.
Code:
admin@jxn-ux-bar01:~/scripts> date -d "12 Aug 2012" +%s
1344747600


Last edited by Franklin52; 09-25-2012 at 07:10 AM.. Reason: Please use code tags for data and code samples, thanks
# 6  
Old 09-20-2012
How fortunate, someone installed GNU date. Smilie

Working on it.
# 7  
Old 09-20-2012
Makes it play well with others :-D
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

List 2 weeks older file on specific directory

Ive been a vocal of FIND command even before. Command below doesnt really give me the file that is older than two weeks.. Is there a script that will list me the log files that i want like for this date December 10, 2014, it shud list me the date between November 26, 2014 and below. When i run... (6 Replies)
Discussion started by: kenshinhimura
6 Replies

2. Shell Programming and Scripting

[Solved] How can I pull specific information from PS?

I need to grab information from the output of the ps command. For each line of ps output that contains _progres -b I need to get the word that follows -p. The "-p" can be anywhere after "_progres -b". Using grep to select the correct lines is no problem (e.g. ps -ef|grep "_progres \-b|grep -v... (3 Replies)
Discussion started by: Papa Lee
3 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Delete files older than 10 years

I need a command which delete files older than 10 years. I got a command for 90 days and all commands I find are for days and nothing for years. find file_name -mtime +90 -exec rm {} \; (5 Replies)
Discussion started by: eskay
5 Replies

4. Shell Programming and Scripting

Move log files with date and delete older than 3 weeks

I have written a script which generate one logfile on every sunday and thursday I want to move the older log files into /tmp directory befor generating new one so i used mv command like mv usr/sbin/appl/logfile.txt usr/sbin/appl/tmp 2) But when i move this file to /tmp it will... (1 Reply)
Discussion started by: Nakul_sh
1 Replies

5. UNIX for Dummies Questions & Answers

Pull out multiple lines with grep patternfile

Hi, I'm trying to get lines from a file using identifiers in the first two columns. I have used: cat MasterFile.txt | grep -f Pattern.txt and the lines I want display on screen. If I try to put them in a file the file is created but stays empty: cat MasterFile.txt | grep -f Pattern.txt... (14 Replies)
Discussion started by: FGPonce
14 Replies

6. Shell Programming and Scripting

[Solved] Remove file older than 90 days

I have crontab job a tar file to a directory ( tar -cvf /tmp/backup/or.`date +%m%d%y`. /ora/db/* ) , it will do it every day . Now I don't want to keep too much files , I just want to keep the file for 90 days , can advise if I want to remove the backup file which are elder than 90 days , can... (1 Reply)
Discussion started by: ust3
1 Replies

7. Shell Programming and Scripting

Pull specific lines from long file based on formula

For people who want to pull a number of lines from a long file using a specific formula n (number of iterations in a loop) a (offset number) b (stretch factor) example with n {1..100} for (( n=1; n<101; n++ )); do awk -v n=$n 'NR==a+(b*n)' a=0 b=1 inputfile >>outputfile (2 Replies)
Discussion started by: sgruenwald
2 Replies

8. Shell Programming and Scripting

How do you use pull data from multiple lines to do a for statement?

Guys I am having a problem with being able to find missing monitors in a configuration check script I am trying to create for accountability purposes for managing a large number of systems. What I am trying to do is run a script that will look at the raw config data in a file and pull all the pool... (7 Replies)
Discussion started by: scottzx7rr
7 Replies

9. Shell Programming and Scripting

Delete the files older than 3 weeks in a particular directory.

Hi, Friends, I am writing a script to delete all the files which are there for more than 3 weeks. I have tried this : find /home/appl/backup -type f -mtime +21 -exec rm -f {} \; But i am not sure if it deletes only the files in specified directory or all the directorinies in the provieded... (3 Replies)
Discussion started by: rajsharma
3 Replies

10. Shell Programming and Scripting

how to check whether the given file is 5 weeks older than current date

HI, I need to check whether the given file is 5 weeks older than current date ?? Can anyone give me the script for this ?? (1 Reply)
Discussion started by: risshanth
1 Replies
Login or Register to Ask a Question