0

I'm using an awk script to print the oldest and newest files created based on the date (the 6th 7th and 8th field).
my sample list, date in the format (YYYY/MM/DD):

file was created 2020/10/10 20:18:42 its name is output1
file was created on 2020/09/10 12:13:22 its name is foobar.awk
file was created on 2020/10/10 20:12:43 its name is output2
file was created on 2020/12/10 18:11:38 its name is foobar.bash
file was created on 2020/12/10 22:32:13 its name is output.txt

what it should look like:

Oldest file date :
2020/09/10 12:13:22
file name: foobar.awk

Newest file date : 2020/12/10 22:32:13 file name: output,txt

Note: I can only use awk I only want the oldest and newest files to be printed.
Also: I am NOT sorting my files, just simply printing the oldest and newest files.

bull
  • 61
  • 2
    This will never work well. You can't depend on the format of ls and it is generally a bad idea to parse the output of ls. See https://mywiki.wooledge.org/ParsingLs and Why *not* parse `ls` (and what to do instead)?. Would you be open to other solutions, using something like stat instead of ls? – terdon Oct 11 '20 at 21:33
  • I've edited to the format to be used with the modified ls -la --time-style – bull Oct 11 '20 at 22:02
  • This really won't work. Just make a file with a space in its name and try running your script. You really want to look at stat instead of ls for this. – terdon Oct 11 '20 at 22:09
  • ls -ltr --time-style=full-iso | sed -n '2p;$p'? – Cyrus Oct 11 '20 at 22:36
  • 1
    Likewise, since you "know about" ls -t, is there any reason you couldn't just do something like ls -tr | awk 'NR==1{print} END{print}'? – steeldriver Oct 11 '20 at 22:37
  • dont worry about piping in ls anymore, I made changes to the file format so that it can act like a regular text file. – bull Oct 11 '20 at 23:08

2 Answers2

0

Assuming GNU awk:

gawk '
    {created[$NF] = $5 " " $6}
    END {
        PROCINFO["sorted_in"] = "@val_str_asc"
        for (f in created) {print "oldest:", f, created[f]; break}
        PROCINFO["sorted_in"] = "@val_str_desc"
        for (f in created) {print "newest:", f, created[f]; break}
    }
' file

Ref: Controlling Array Traversal, and Using Predefined Array Scanning Orders with gawk

glenn jackman
  • 85,964
0

Using stat to get the files and their modification times, awk to get min/max without sorting and format the output.

stat -c "%Y %n" * | awk '
    min == "" || min > $1 {min = $1; fmin = substr($0, index($0, FS)+1)}
    max == "" || max < $1 {max = $1; fmax = substr($0, index($0, FS)+1)}
    END {
        form = "%Y/%m/%d %H:%M:%S"
        printf "Oldest\nDate: %s\nFile: %s\n", strftime(form, min), fmin
        printf "Newest\nDate: %s\nFile: %s\n", strftime(form, max), fmax
    }'

Sample output:

Oldest
Date: 2020/08/29 01:52:23
File: test.java
Newest
Date: 2020/10/12 06:52:21
File: test.sh

Notes:

  • The above handles whitespaces into files, not newlines.
  • I think awk strftime is GNU only
thanasisp
  • 8,122