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.
ls
and it is generally a bad idea to parse the output ofls
. 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 likestat
instead ofls
? – terdon Oct 11 '20 at 21:33ls -la --time-style
– bull Oct 11 '20 at 22:02stat
instead ofls
for this. – terdon Oct 11 '20 at 22:09ls -ltr --time-style=full-iso | sed -n '2p;$p'
? – Cyrus Oct 11 '20 at 22:36ls -t
, is there any reason you couldn't just do something likels -tr | awk 'NR==1{print} END{print}'
? – steeldriver Oct 11 '20 at 22:37ls
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