Operating System - Linux
1752562 Members
4649 Online
108788 Solutions
New Discussion юеВ

ksh script Q about removing entries from a list in a VAR

 
SOLVED
Go to solution
Eric SAUBIGNAC
Honored Contributor

Re: ksh script Q about removing entries from a list in a VAR

I find Sandman! post really attractive as it gives the answer with the reverse order that Steve wanted ;-)

Of course NO POINT

Eric
Steve Post
Trusted Contributor

Re: ksh script Q about removing entries from a list in a VAR

Ok let me summarize.....
one way: ls -1tr | sed '$d' | sed '$d'
I don't like that I pipe to sed twice.

one way: ls -1t | tail -n +3
The +3 means to tail line 3 and up. (tail -n -3 would be the last 3 lines).
If the number of lines in the ls command is a very large number, the tail would fail. But this will be a small number of files.

one way: ls -1tr | awk '{x[NR]=$0} END {for(i=1;iThis awk script is nice. But I wouldn't use it until I knew people looking at it would understand it. And I would hope it worked on all versions of awk.

For me, I like the tail idea the best. But I REALLY like the fact that there is more than one way to do it.

Thank you all.

Eric SAUBIGNAC
Honored Contributor

Re: ksh script Q about removing entries from a list in a VAR

You said "But I wouldn't use it until I knew people looking at it would understand it. ". So I make the post for Sandman! as he can't control himself !

{x[NR]=$0}

==> For each line initialize an array (x) with the line itself ($0). NR is the current line number

END {for(i=1;i
==> at end of input stream, that is ls -1tr, just print all lines but the two last

It will work ... assuming that number of files listed is not greater that the limit of awk on array sizes.

No needs to give POINT

Regards

Eric
James R. Ferguson
Acclaimed Contributor

Re: ksh script Q about removing entries from a list in a VAR

hi Steve:

So, if 'tail', sed' and 'awk' are OK for the filters, why not Perl?

# ls -1tr|perl -nle 'push @a,$_;END{print "@a[2..$#a]"}'

After all, TMTOWTDI.

Regards!

...JRF..