Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
cancel
Showing results for 
Search instead for 
Did you mean: 

Script Help

SOLVED
Go to solution
Highlighted
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
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.  ;-)