this is the actual pseudocode for the script I am developing
log files are created by timestamp, so need to extract unique time stamps and save in a list file.. EXCEPT the latest two, then I need to archive those files in an Archive folder (moving). so that only the latest two remains in the .LOG folder.
so I am doing. (awk '{print substr($9,1,14)} as filename is of 14 char.)
cd $LOGDIR
ls -ltr *.log |awk '{print substr($9,1,14)}'|sort -u|!(tail -2) > filenameslist
I tried the following command suggested by one of the guru.. but it is throwing me an error.
I did not expect you do sort on $9 filed and then apply second awk.
by doing this,your order is getting changed and you are ignoring your
requirement of not to include 2 latest files.
also in previous posting...
i put "ls -lt" not "ls -ltr"
================================
Rather , if you change your order of commands you get results expected.
Try the following.
Following is working for me on simple files (not *.log)
ls -lt | awk '{ if (NR > 3) print $0}' | awk '{ print substr($9,1,14) }' | sort -u
if you just want the filenames (and do not need the other fields), you can just do this:
ls -t *log|tail +3
this will give all files except the latest two. besides, awk is slower than this