1745808 Members
3826 Online
108722 Solutions
New Discussion

Script Help

 
SOLVED
Go to solution
allanm77
Frequent Advisor

Script Help

I have this following script which lists out packages on a filesystem (but NOT the latest package) on a filesystem.

 

I want to avoid listing out the latest 3 packages instead of one.

 

ls -l | awk '
BEGIN { prev = ""; app = "" }
{
n = split($9, apps, "-")
if (n == 0) next
if (n < 2) {
   print "problem getting appname for", $9
   next
}
appname = apps[1]
if (appname == app) {
   print prev  # older
   prev = $9   # save
   next
}
app = appname
prev = $9
} '

 

 

 

Thanks,

Allan.

4 REPLIES 4
Dennis Handly
Acclaimed Contributor

Re: awk script help

>I want to avoid listing out the latest 3 packages instead of one.

 

I suppose you could have multiple "prev" buffers and shuffle them down.

Or create a circular buffer.

BEGIN { app = "" }

...

if (appname == app) {

   if (prev[nn] != "")

      print prev[nn]  # older

   prev[nn++] = $9   # save

   if (nn >=3) nn = 0
   next
}
app = appname

# flush prev array

for (nn in prev) delete prev[nn]

nn = 0
prev[nn++] = $9 # save
} '

allanm77
Frequent Advisor

Re: awk script help

Thanks Dennis, for the reply.

 

I tried the circular buffer but that shows the latest packge.

 

Allan.

 

Dennis Handly
Acclaimed Contributor

Re: awk script help

>I tried the circular buffer but that shows the latest package.

 

Hmm, I'll have to check it out later to day.

Have you tried only 1, 2, 3, 4, 5, 6 packages and see what happens?

Dennis Handly
Acclaimed Contributor
Solution

Re: awk script help

>I tried the circular buffer but that shows the latest package.

 

Rats!  A conflict in your variable "n" and mine.  I've updated it to use "nn".

It seems to work fine.

 

BTW.  If you pipe the result of your awk script 3 times, you can do the same thing.  ;-)