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
-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:
-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
Try:
/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
1 Like
You are unbelievable in so Many ways...
---------- Post updated at 07:33 AM ---------- Previous update was at 07:23 AM ----------
It will be realy great if you could explain above command...
Thanks ... I'll try to explain:
/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
1 Like
Hi Scrutinizer,
Above script is failing in lagre file set.
Could you please provide script by actualy comparing dates rather than position
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
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
Hi, on Solaris 10 I am getting:
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?
Yes.
With nawk it is working fine. Thanks again.