Re: awk script help (382 Views)
Reply
Frequent Advisor
allanm77
Posts: 88
Registered: ‎06-27-2011
Message 1 of 5 (432 Views)
Accepted Solution

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.

Acclaimed Contributor
Dennis Handly
Posts: 25,274
Registered: ‎03-06-2006
Message 2 of 5 (416 Views)

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
allanm77
Posts: 88
Registered: ‎06-27-2011
Message 3 of 5 (406 Views)

Re: awk script help

Thanks Dennis, for the reply.

 

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

 

Allan.

 

Acclaimed Contributor
Dennis Handly
Posts: 25,274
Registered: ‎03-06-2006
Message 4 of 5 (382 Views)

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
Dennis Handly
Posts: 25,274
Registered: ‎03-06-2006
Message 5 of 5 (373 Views)

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

The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.