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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK (how) to get smallest/largest nr of ls -la
# 1  
Old 04-12-2010
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:
Code:
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 Smilie. 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:
Code:
min=max=0
{ 
   if($5 > max)
   max++
else if
 
and now I'm already "brain-frozen":(...


Last edited by abciscool; 04-12-2010 at 07:06 AM.. Reason: Adding context.
# 2  
Old 04-12-2010
Yo dont need awk for this:. You can use the output of ls -lrt or ls -s

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

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

Code:
 ls -lrt | grep -v "^d" | grep -v grep | grep -v "total" | head -1

Code:
 ls -lrt | grep -v "^d" | grep -v grep | grep -v "total" | tail -1

-rw-rw-r--
# 3  
Old 04-12-2010
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 Smilie.

Hopefully someone can help me, though I really do appreciate the quick answer devtakh.
# 4  
Old 04-12-2010
Ok.

Code:
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
# 5  
Old 04-12-2010
Quote:
Originally Posted by devtakh
Ok.

Code:
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!! Smilie).

To recap, for thought-process:

Code:
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!!)
# 6  
Old 04-12-2010
Quote:
Originally Posted by abciscool
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!! Smilie).

To recap, for thought-process:

Code:
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to combine all matching fields in input but only print line with largest value in specific field

In the below I am trying to use awk to match all the $13 values in input, which is tab-delimited, that are in $1 of gene which is just a single column of text. However only the line with the greatest $9 value in input needs to be printed. So in the example below all the MECP2 and LTBP1... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

Print smallest integer from file using awk custom function?

`awk` function looks like this in a file name `fun.awk`: { print small() } function small() { a=$0 smal=0 for(i=1;i<=3;i++) { if( a<a) smal=a else (4 Replies)
Discussion started by: lazerz
4 Replies

3. Shell Programming and Scripting

Find smallest between replicates ID

Hi All I need to find the smallest values between replicates id (column1) Input file: a name1 1200 a name2 800 b name1 100 b name2 150 b name3 4output: a name2 800 b name3 4 Do you have any suggestion? Thank you! (9 Replies)
Discussion started by: giuliangiuseppe
9 Replies

4. Shell Programming and Scripting

Find smallest & largest in every column

Dear All, I have input like this, J_15TEST_ASH05_33A22.13885.txt: $$ 1 MAKE SP1501 1 1 4 6101 7392 2 2442 2685 18 3201 4008 20 120 4158 J_15TEST_ASH05_33A22.13885.txt: $$ 1 MAKE SP1502 1 1 4 5125 6416 2 ... (4 Replies)
Discussion started by: attila
4 Replies

5. Shell Programming and Scripting

Use a string in one column to get the largest or the smallest of another column

I have data that looks like this: chr1 mm9_knownGene exon 155747075 155747189 0.000000 + . gene_id "Glul"; transcript_id "uc007daq.1"; chr1 mm9_knownGene exon 155750064 155750076 0.000000 + . gene_id "Glul";... (3 Replies)
Discussion started by: pbluescript
3 Replies

6. UNIX for Dummies Questions & Answers

Printf awk: argument to adjust automatically width to the largest value

Hi ! Here is an example of a tab-delimited file with variable width values in field 2 (space separated values). As the real file contains lot of lines, I can adjust manually the width to the largest value in field 2. Does it exist an argument to do it automatically? input.tab:... (4 Replies)
Discussion started by: lucasvs
4 Replies

7. Shell Programming and Scripting

awk second largest, third largest value

I have two text files like this: file1.txt: 133 10 133 22 133 13 133 56 133 78 133 98 file2.txt: 158 38 158 67 158 94 158 17 158 23 I'm basically trying to have awk check the second largest value of the second column of each text file, and cat it to its own text file. There... (13 Replies)
Discussion started by: theawknewbie
13 Replies

8. Shell Programming and Scripting

largest field , awk , help

Hi All, My file is like this: $ cat max.txt abcd:1982:a efghij:1980:e klmn:1923:k opqrst:1982:o I have to find out the largest first field and the corresponding line. i.e Output required: efghij efghij:1980:e opqrst opqrst:1982:o HTH, jkl_jkl (6 Replies)
Discussion started by: jkl_jkl
6 Replies

9. UNIX for Dummies Questions & Answers

How to print largest and smallest number.

Hey. This is pretty easy stuff but I'm learning the basics of Unix at the moment so keep that in mind. I have to: 1) Write a C-shell script to monitor user activity on the server for 13 minutes. 2) Then print the smallest and largest number of users during these 13 minutes. I have this: 1)... (2 Replies)
Discussion started by: amp10388
2 Replies

10. Shell Programming and Scripting

checking the smallest and largest number

Hi All, My script is reading a log file line by line log file is like ; 19:40:22 :INFO Total time taken to Service External Request---115ms 19:40:25 DEBUG : Batch processed libdaemon.x86_64 0-0.10-5.el5 - u 19:40:22 INFO Total time taken to Service External Request---20ms 19:40:24... (4 Replies)
Discussion started by: subin_bala
4 Replies
Login or Register to Ask a Question