How to find all files other than first two dates & last date per month and year?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to find all files other than first two dates & last date per month and year?
# 1  
Old 04-25-2014
How to find all files other than first two dates & last date per month and year?

how to find all files other than first two dates & last date per month and year

Hi All,
lets say there are following files in directory

Code:
-rwxr-xr-x   1 user  userg         1596 Mar 19 2012 a.txt
-rwxr-xr-x   1 user  userg         1596 Mar 19 2012 b.txt
-rwxr-xr-x   1 user  userg         1596 Mar 22 2012 c.txt
-rwxr-xr-x   1 user  userg         1596 Mar 24 2012 d.txt
-rwxr-xr-x   1 user  userg         1596 Mar 25 2012 e.txt
-rwxr-xr-x   1 user  userg         1596 Mar 27 2012 ee.txt
-rwxr-xr-x   1 user  userg         1596 Feb 12 2012 f.txt
-rwxr-xr-x   1 user  userg         1596 Feb 12 2012 g.txt
-rwxr-xr-x   1 user  userg         1596 Feb 22 2012 h.txt
-rwxr-xr-x   1 user  userg         1596 Feb 23 2012 i.txt
-rwxr-xr-x   1 user  userg         1596 Feb 28 2012 j.txt
-rwxr-xr-x   1 user  userg         1596 Feb 28 2012 jj.txt
-rwxr-xr-x   1 user  userg         1596 Apr 02 2013 k.txt
-rwxr-xr-x   1 user  userg         1596 Apr 11 2013 l.txt
-rwxr-xr-x   1 user  userg         1596 Apr 11 2013 m.txt
-rwxr-xr-x   1 user  userg         1596 Apr 23 2013 n.txt
-rwxr-xr-x   1 user  userg         1596 Apr 27 2013 o.txt
-rwxr-xr-x   1 user  userg         1596 Apr 29 2013 oo.txt

i want all files showed in red color

for Mar 2012
first two dates are Mar19 & Mar22
last 1 date is Mar27

for Feb 2012
first two dates are Feb12 & Feb22
last 1 date is Feb28

for Apr 2013
first two dates are Apr02 & Apr11
last 1 date is Apr29

So i want all files in directory other than above dates

output should be:

Code:
-rwxr-xr-x   1 user  userg         1596 Mar 24 2012 d.txt
-rwxr-xr-x   1 user  userg         1596 Mar 25 2012 e.txt
-rwxr-xr-x   1 user  userg         1596 Feb 23 2012 i.txt
-rwxr-xr-x   1 user  userg         1596 Apr 23 2013 n.txt
-rwxr-xr-x   1 user  userg         1596 Apr 27 2013 o.txt

Any help much appreciated!!!

OS: SunOS 5.8 & KSH

Last edited by Makarand Dodmis; 04-25-2014 at 07:57 AM..
# 2  
Old 04-25-2014
Try:
Code:
/usr/xpg4/bin/awk '$6!=m{m=$6; c=0} {if($7!=d){if(c++>n)print b p; b=x} else b=b p ORS} {p=$0; d=$7}' n=2  file


Last edited by Scrutinizer; 04-25-2014 at 08:59 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 04-25-2014
You are unbelievable in so Many ways...Smilie

---------- Post updated at 07:33 AM ---------- Previous update was at 07:23 AM ----------

It will be realy great if you could explain above command...
# 4  
Old 04-25-2014
Thanks Smilie ... I'll try to explain:
Code:
/usr/xpg4/bin/awk '                        # Use the POSIX compliant version of awk on Solaris 8
  $6!=month {                              # if the 6th field ($6) is unequal to the month of the previous line
    month=$6                               # then month becomes $6
    count=0                                # count (the number of entries from the last month) is reset to 0
  }
  {
    if($7!=day) {                          # if $7 is unequal to the previous entry's day
      if(count++>n) print buffer previous  # then if the count is larger than n (increase count after the comparison)
                                           # then print the buffer concatenated with the previous line 
      buffer=x                             # and clear the buffer
    }
    else                                   # if $7 is equal to the previous entry's day
      buffer=buffer previous ORS           # then add the previous record to the buffer followed by an output record separator
  }
  {
    previous=$0                            # set "previous" equal to the current record
    day=$7                                 # set the day equal to day of the current record
  }
' n=2  file                                # set the threshold to start printing in a new month to 2 and read the file


Last edited by Scrutinizer; 04-25-2014 at 12:05 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 04-25-2014
Hi Scrutinizer,

Above script is failing in lagre file set.
Could you please provide script by actualy comparing dates rather than position

Code:
drwxr-xr-x   2 user  userg         1024 Sep  3  2013 130902
drwxr-xr-x   2 user  userg         1024 Sep  4  2013 130903
drwxr-xr-x   2 user  userg         1024 Sep  5  2013 130904
drwxr-xr-x   2 user  userg         1024 Sep  6  2013 130905
drwxr-xr-x   2 user  userg         1024 Sep  7  2013 130906
drwxr-xr-x   2 user  userg         1024 Sep 10  2013 130909
drwxr-xr-x   2 user  userg         1024 Sep 11  2013 130910
drwxr-xr-x   2 user  userg         1024 Sep 12  2013 130911
drwxr-xr-x   2 user  userg         1024 Sep 13  2013 130912
drwxr-xr-x   2 user  userg         1024 Sep 14  2013 130913
drwxr-xr-x   2 user  userg         1024 Sep 17  2013 130916
drwxr-xr-x   2 user  userg         1024 Sep 18  2013 130917
drwxr-xr-x   2 user  userg         1024 Sep 19  2013 130918
drwxr-xr-x   2 user  userg         1024 Sep 20  2013 130919
drwxr-xr-x   2 user  userg         1024 Sep 21  2013 130920
drwxr-xr-x   2 user  userg         1024 Sep 24  2013 130923
drwxr-xr-x   2 user  userg         1024 Sep 25  2013 130924
drwxr-xr-x   2 user  userg         1024 Sep 26  2013 130925
drwxr-xr-x   2 user  userg         1024 Sep 27  2013 130926
drwxr-xr-x   2 user  userg         1024 Sep 28  2013 130927

output coming & which is wrong

Code:
drwxr-xr-x   2 user  userg         1024 Sep  5  2012 120904
drwxr-xr-x   2 user  userg         1024 Sep  6  2012 120905
drwxr-xr-x   2 user  userg         1024 Sep  7  2012 120906
drwxr-xr-x   2 user  userg         1024 Sep  8  2012 120907
drwxr-xr-x   2 user  userg         1024 Sep 11  2012 120910
drwxr-xr-x   2 user  userg         1024 Sep 12  2012 120911
drwxr-xr-x   2 user  userg         1024 Sep 13  2012 120912
drwxr-xr-x   2 user  userg         1024 Sep 28  2012 120927

# 6  
Old 04-25-2014
Hi, on Solaris 10 I am getting:
Code:
drwxr-xr-x   2 user  userg         1024 Sep  5  2013 130904
drwxr-xr-x   2 user  userg         1024 Sep  6  2013 130905
drwxr-xr-x   2 user  userg         1024 Sep  7  2013 130906
drwxr-xr-x   2 user  userg         1024 Sep 10  2013 130909
drwxr-xr-x   2 user  userg         1024 Sep 11  2013 130910
drwxr-xr-x   2 user  userg         1024 Sep 12  2013 130911
drwxr-xr-x   2 user  userg         1024 Sep 13  2013 130912
drwxr-xr-x   2 user  userg         1024 Sep 14  2013 130913
drwxr-xr-x   2 user  userg         1024 Sep 17  2013 130916
drwxr-xr-x   2 user  userg         1024 Sep 18  2013 130917
drwxr-xr-x   2 user  userg         1024 Sep 19  2013 130918
drwxr-xr-x   2 user  userg         1024 Sep 20  2013 130919
drwxr-xr-x   2 user  userg         1024 Sep 21  2013 130920
drwxr-xr-x   2 user  userg         1024 Sep 24  2013 130923
drwxr-xr-x   2 user  userg         1024 Sep 25  2013 130924
drwxr-xr-x   2 user  userg         1024 Sep 26  2013 130925
drwxr-xr-x   2 user  userg         1024 Sep 27  2013 130926

Try using nawk instead of /usr/xpg4/bin/awk . Otherwise perhaps use gawk if that happens to be installed.. I suspect there may be a problem with your particular awk version. I do not have Solaris 8 at my disposal, which is ancient of course...

---
edit there was a glitch in the explanatory version. I edited it in my post. Perhaps that was the problem?

Last edited by Scrutinizer; 04-25-2014 at 12:11 PM..
# 7  
Old 04-25-2014
Yes.
With nawk it is working fine. Thanks again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How bash treats literal date value and retrieve year, month and date?

Hi, I am trying to add few (say 3 days) to sysdate using - date -d '+ 3 days' +%y%m%d and it works as expected. But how to add few (say 3 days) to a literal date value and how bash treats a literal value as a date. Can we say just like in ORACLE TO_DATE that my given literal date value... (2 Replies)
Discussion started by: pointers1234
2 Replies

2. Shell Programming and Scripting

How to find all files other than last two dates per month and year?

Hi All, lets say there are following files in directory -rwxr-xr-x 1 user userg 1596 Mar 19 2012 a.txt -rwxr-xr-x 1 user userg 1596 Mar 19 2012 b.txt -rwxr-xr-x 1 user userg 1596 Mar 22 2012 c.txt -rwxr-xr-x 1 user userg 1596 Mar 24 2012 d.txt... (16 Replies)
Discussion started by: Makarand Dodmis
16 Replies

3. Shell Programming and Scripting

How to find all files for same month and year?

Hi All, I find all files for same month and year lets say there are following files in directory -rwxr-xr-x 1 user userg 1596 Mar 19 2012 c.txt -rwxr-xr-x 1 user userg 1596 Mar 21 2012 d.txt -rwxr-xr-x 1 user userg 1596 Mar 22 2012 f.txt -rwxr-xr-x 1... (8 Replies)
Discussion started by: Makarand Dodmis
8 Replies

4. UNIX for Advanced & Expert Users

Find all files other than first two files dates & last file date for month

Hi All, I need to find all files other than first two files dates & last file date for month and month/year wise list. lets say there are following files in directory Mar 19 2012 c.txt Mar 19 2012 cc.txt Mar 21 2012 d.txt Mar 22 2012 f.txt Mar 24 2012 h.txt Mar 25 2012 w.txt Feb 12... (16 Replies)
Discussion started by: Makarand Dodmis
16 Replies

5. Shell Programming and Scripting

How to list files that are not first two files date & last file date for every month-year?

Hi All, I need to find all files other than first two files dates & last file date for month and month/year wise list. lets say there are following files in directory Mar 19 2012 c.txt Mar 19 2012 cc.txt Mar 21 2012 d.txt Mar 22 2012 f.txt Mar 24 2012 h.txt Mar 25 2012 w.txt Feb 12... (2 Replies)
Discussion started by: Makarand Dodmis
2 Replies

6. Shell Programming and Scripting

Julian day to dates in YEAR-MONTH-DAY

hello, I have many files called day001, day002, day003 and I want to rename them by day20070101, day20070102, etc. I need to do it for several years and leap years as well. What is the best way to do it ? Thank you. (1 Reply)
Discussion started by: Ggg
1 Replies

7. UNIX for Dummies Questions & Answers

Unix man command to find out month of the year?

how can i display month of the year i was born with using man command? thanks (2 Replies)
Discussion started by: janetroop95
2 Replies

8. Shell Programming and Scripting

how to increment days according to year & month

Hiii i have a file with data as shown below: a.dat: RAO 1900 2 7 0 0 0.00 10.8000 76.8000 10.0 0 0.00 0 6.00 0.00 0.00 0 0.00 6.00 0 NULL LEE 1901 2 15 0 0 0.00 26.0000 100.0000 0.0 0 0.00 0 0.00 0.00 0.00 0 6.00 6.00 0 NULL RAO 1901 4... (3 Replies)
Discussion started by: reva
3 Replies

9. Shell Programming and Scripting

Get yesterday's date in year-month-day format?

Dear All, Actually, i'm doing some reporting job and i need to pass yesterday's date in Year-Month-Day format(e.g. 2009-06-10) to another program for generating 2009-06-10 report. to get today's date, it's easy to just date '+%Y%m%d' , but no idea how can i get this kind of format for... (2 Replies)
Discussion started by: tiger2000
2 Replies

10. Shell Programming and Scripting

Script for tar and zip based on month & year

Hi Friends, I'm doing on script which finds all the files with time stamp and makes them tar and zip, based on their respective month&year. for instance "mar-2004.tar.zip" will contain all the files which was created/accessed/modified on mar-2004. like this the entire filesystem should be taken... (1 Reply)
Discussion started by: tuxfello
1 Replies
Login or Register to Ask a Question