BladeSystem Forums Have Moved here
To make BladeSystem information easier to find, we have moved the BladeSystem forums here, to Servers and Operating Systems.
Showing results for 
Search instead for 
Do you mean 

Script Help

SOLVED
Go to Solution
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
Acclaimed Contributor

Re: awk script help

[ Edited ]

>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
} '

Frequent Advisor

Re: awk script help

Thanks Dennis, for the reply.

 

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

 

Allan.

 

Highlighted
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?

Acclaimed Contributor

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.  ;-)