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


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users help with !(tail -2) command.. using pipes
# 1  
Old 02-15-2005
Network 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
# 2  
Old 02-15-2005
Try this:


ls -ltr *.lst|tail -r|tail +3|tail -r
# 3  
Old 02-15-2005
i do like this ...

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

# 4  
Old 02-15-2005
Network Thanks guys.. but still I am not getting the correct result

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
# 5  
Old 02-15-2005
MySQL Thank you all.. but is there any better way. avoid Awk for the second time

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..
# 6  
Old 02-17-2005
why use awk? just use ls -lt|tail +4 and you are there.
# 7  
Old 02-17-2005
sdlayeeq !

I see following errors with the following ...

Quote:
ls -ltr *.log |awk 'print substr($9,1,14)}'|sort -u|awk '{if (NR >3) print $0}'
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)

Code:
ls -lt | awk '{ if (NR > 3) print $0}' | awk '{ print substr($9,1,14) }' | sort -u

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Limit on number of pipes after long-running command?

I'm trying to create a minimal, crude keylogger for X using only a shell script. I was quickly stumped: Why do these two commands entered in a terminal emulator produce output when I type... $ xinput test 6 | grep press $ xinput test 6 | awk '{print $3}' ...but this command produces no... (13 Replies)
Discussion started by: DevuanFan
13 Replies

2. UNIX for Beginners Questions & Answers

Tail -f Command help

Hi Team, Can anyone help me here: I have to access server logs via putty and these logs file is a trailing file (continously updating) with ERROR and WARNINGS... I need to know if I can pull this trailing file to a local drive so that I can do some higlighting on some keywords through Notepad... (13 Replies)
Discussion started by: jitensetia
13 Replies

3. Shell Programming and Scripting

tail command help

Hi does anyone know how to create a file using the tail command? My book has this file I need to create and it says to use the tail command and that it is possible but I have no idea. Thanks. (4 Replies)
Discussion started by: drew211
4 Replies

4. Shell Programming and Scripting

tail command problems

Hi, In my home directory, there are so many files. i tried to get the lastly created file by following command. file=`ls -lrt MXOfiles* | tail -1` As there are so many files, it displays "$ : 0403-027 The parameter list is too long." Can someone tell me how can i get the recent file... (1 Reply)
Discussion started by: JSKOBS
1 Replies

5. Shell Programming and Scripting

Using pipes in a find command

How are pipes used inside a find -exec clause? I want to traverse a directory tree and find all of the shell scripts, and then based on that I need to perfrom further processing. So I have something like find . -name \* -exec "file {} \| grep 'script' > /dev/null" \; -ls with -ls as a simple... (6 Replies)
Discussion started by: JerryHone
6 Replies

6. Shell Programming and Scripting

Help with tail command

Hi All, My query seems to be silly but Iam unable to find where the exact problem lies. I have a script to unzip set of files here is the script #!/bin/ksh Count=`cat /home/gaddamja/Tempfile | wc -l` while do Filename=`cat /home/gaddamja/Tempfile |tail -$Count | head -1` cd... (7 Replies)
Discussion started by: jagadish_gaddam
7 Replies

7. Shell Programming and Scripting

Multiple pipes toward a single awk command

Hello, I would like to pipe two variables into awk, but I don't know how to do. Each variable, "a" and "b", are in fact a list of data. They are not files. So to get awk to work with it I am using: echo $a | awk 'FNR==NR{print $1}FNR!=NR{print $4}' The above works, but when I am... (5 Replies)
Discussion started by: jolecanard
5 Replies

8. Shell Programming and Scripting

tail command..

I was wondering how can I do this I have file myfile.txt wc -l is: 5 000 000 I have to remove first 1 000 000 lines from header.. I tryed with tail -4000000 myfile.txt>newfile.txt but it does not work... any help?? (2 Replies)
Discussion started by: amon
2 Replies

9. Shell Programming and Scripting

tail command

Hi , I have found a interesting thing about tail command: when I tried to use 'tail -1 *' to look at every file with the current derectory, I only got one line of result of one file. But if I use 'head -1 *', I would get multiple lines. Is there a way to do get multiple lines with 'tail -1 *'... (3 Replies)
Discussion started by: whatisthis
3 Replies

10. Filesystems, Disks and Memory

PIPEs and Named PIPEs (FIFO) Buffer size

Hello! How I can increase or decrease predefined pipe buffer size? System FreeBSD 4.9 and RedHat Linux 9.0 Thanks! (1 Reply)
Discussion started by: Jus
1 Replies
Login or Register to Ask a Question