help with !(tail -2) command.. using pipes

I am trying to using pipe (|) with ! (not) operator to list all the other files except the latest two and I am using the following command.

$ ls -ltr *.lst|!(tail -2)
ksh: 20050211180252.lst: cannot execute

but it is trying to execute the file returned by tail -2. I am able to do that in 4 steps

1) ls -ltr *.lst > listfile
2) numberoflines=`wc -l | cut -d" "`
3) ((numberoflines = numberoflines - 2))
4) head -$numberoflines > final_list.

Are there any Unix gurus who could help me write the above commands using pipe facility. I am using Korn shell.

thanks in advance

Try this:

ls -ltr *.lst|tail -r|tail +3|tail -r

i do like this ...

ls -lt | awk '{ if (NR >3) print $0 }'

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.

ls -ltr *.log |awk 'print substr($9,1,14)}'|sort -u|awk '{if (NR >3) print $0}' > filenameslist

Thanks a lot guys for the valuable thougts.. can any body have a better Idea.

challenge is to get it done using pipe.. I mean in one line

Thanks to all the guru's. but I thought is there a way to use
!(tail -2)..

can I avoid Awk for the second time Bhargav.. thanks in advance..

why use awk? just use ls -lt|tail +4 and you are there.

sdlayeeq !

I see following errors with the following ...

  1. syntax error { is missing for the first awk.
  2. 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.
  3. 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

You can combine 2 awk steps into one .....


ls -lt *.log | awk '{ if (NR > 3) 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