AWK (how) to get smallest/largest nr of ls -la

Hey,

This is a long-shot however, I am stuck with the following problem:

I have the output from ls -la, and I want to sort some of that data out by using AWK to filter it.

ls -la | awk -f scriptname.awk
Input:

For example:
drwxr-xr-x 3 user users  4096 2010-03-14 20:15  bin/
drwxr-xr-x 2 user users  4096 2010-03-14 21:15  Desktop/
-rw-r--r-- 1 user users    81 2010-03-14 22:15  dirs.txt
drwxrwxrwx 2 user users  4096 2010-03-14 23:15  pub/
-rw-r--r-- 1 user users 13910 2010-03-14 0:15   y.txt
etc... 

I wanted to sort out:

  • The smallest filesize ($5)
  • The biggest filesize ($5)
  • The oldest file ($6 & $7?)
  • The newest file ($6 & $7?)

I'm really sorry and ashamed that I can't make this script myself. I've been trying to figure it out for the last day, and I just can't :(. I really tried a lot of options, and also the old-fashion way of writing in text what the program should do and then write it out in code, however I just don't know where to start. I guess my sense of logic is failing miserably.

I really, really hope someone can help me out.

  • Mario de Rooy (Netherlands)

---------- Post updated at 09:58 AM ---------- Previous update was at 09:52 AM ----------

My general logic was:

min=max=0
{ 
   if($5 > max)
   max++
else if
 
and now I'm already "brain-frozen":(...

Yo dont need awk for this:. You can use the output of ls -lrt or ls -s

 ls -lrt | grep -v "^d" | grep -v grep | sort -n +4 | grep -v "total" | tail -1
ls -lrt | grep -v "^d" | grep -v grep | sort -n +4 | grep -v "total" | head -1
 ls -lrt | grep -v "^d" | grep -v grep | grep -v "total" | head -1
 ls -lrt | grep -v "^d" | grep -v grep | grep -v "total" | tail -1

-rw-rw-r--

Thanks for the quick reply, however,

I really want to do it with awk and I think awk is able to produce very good results. But I just can't think of a way to start with it :(.

Hopefully someone can help me, though I really do appreciate the quick answer devtakh.

Ok.

ls -lrt | awk 'NR==2{min=max=$5}NR>2{if (min > $5) min=$5;if (max < $5) max=$5 }END{print "MIN:", min,"Max:",max}'

MIN: 9 Max: 4096

Thank you so much for you help, if it works it will cover already half of what I wanted to achieve (which is a lot!! :D).

To recap, for thought-process:

NR==2 { min = max = $5}
NR > 2 { if (min > $5) 
            min = $5
            if (max < $5)
            max = $5 }
END {print "Minimum:", min, "Maximum:", max}

If I read it correctly;
At the second record set the variables min and max to the value of the 5th field. Then at every record past the second one do;

  • if the min-variable is greater than the value of the 5th field, set the variable min to the current value of the 5th field.
  • if the max-variable is smaller than the value of the 5th field, set the variable max to the current value of the 5th field.
    At the END print the min- & max- variables current value in the output.

Is my clarification of this code correct, or am I missing some aspects?

  • Mario de Rooy

(Thanks again!!)

Mario,

Your assumption is correct.

Regards