Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

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

 
SOLVED
Go to solution
Highlighted
Trusted Contributor

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

I can think of how do this in perl, but I know it should be easily done in ksh.

I have a list of files in a variable.
MYLIST=`ls -1tr logs.*`
(That's one-t-r, not EL-t-r).
Because I ordered the files by time, the last two files on the list are the latest 2 files.

I want to remove the latest two logs from the list.

I can't wrap my brain around how to do this. Then I realized this is a good question for the forums. It would be good to see different methods to handle it.

In perl it would be:
( @goodlist, $last2, $last1) = ( @filelist );
But this isn't perl.

The script would be like this:
MYLIST=`ls -1tr logs.*`
# unknown magic code to remove latest 2 files goes here
for F in $MYLIST
do
rm $F
done

any help would be appreciated.

steve
13 REPLIES 13
Highlighted
Acclaimed Contributor
Solution

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

Hi Steve:

# MYLIST=$(ls -1tr /tmp|tail -2|xargs)

Regards!

...JRF...
Highlighted
Acclaimed Contributor

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

Hi (again) Steve:

Actually, since you just want to compose the list in the variable, you don't need the 'xargs'.

# MYLIST=$(ls -1tr /tmp|tail -2)

Regards!

...JRF...
Highlighted
Trusted Contributor

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

When I tried the answer I see I got the opposite of what I was looking for. It got the two files I want to take off the list. But since they are identified, I have a way to mask them out. Here is my code.

LAST2LIST=`ls -1tr log.?????? | tail -2`
LIST=`ls -1tr log.??????`
echo "orig"
echo "$LIST"
NLIST=
for Y in $LIST
do
Flag=0
for X in $LAST2LIST
do
if [ "$X" = "$Y" ] ; then
Flag=1
fi
done
if [ $Flag -eq 0 ] ; then
NLIST="$NLIST $Y"
fi
done
LIST="$NLIST"
echo "after"
for Y in $LIST
do
echo $Y
done

Perhaps there is a more elegant way to perform this?
Highlighted
Honored Contributor

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

Bonjour Steve,

Do you really need the list in a reverse order ? I mean do you use -r option only as a way to isolate the 2 latest files ?

If so, don't use -r option and have the latest files first. So it becomes simplier to remove them :

MYLIST=`ls -1t logs.* | tail -n +2`

Regards

Eric
Highlighted
Acclaimed Contributor

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

Hi Steve:

I'm sorry, I didn't read your requirement very well!

NO POINTS PLEASE!

Anyway, one way in a shell would be to pipe the 'ls' output to a temporary file. Now, knowing the number of records in the file, loop, reading all but the last two.

By the way, in Perl your example would be:

# perl -le '@list=qw(a b c d e f);($x,$y,@good)=(@list);print "@good"'

In your post, '@goodlist' would greedily suck up all of '@filelist'.

Regards!

...JRF...
Highlighted
Honored Contributor

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

Sorry, did a mistake ;-(

It was :

MYLIST=`ls -1t logs.* | tail -n +3`

Eric
Highlighted
Trusted Contributor

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

Yep. I didn't really try the perl. If I did, I would have caught that.

Now the "tail -n -2" only grabs the last 2 lines. These are the two lines I don't want. I kept trying this and could not get it to work.

BUT.....I found an elegant way after all.

ls -1tr logs.* | sed '$d' | sed '$d'
This gives me the list of logs from earliest to latest. Then sed '$d' removes the bottom line. The second sed '$d' removes the bottom line again (ie the 2nd to last file).

Now perhaps there is something stupid about this I don't see. Maybe there is a sed command to remove the bottom two lines in one command instead of piping it twice?

steve
Highlighted
Acclaimed Contributor

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

Hi Steve:

Eric has the simple solution. See _his_ _second _post_. It's the difference between +n and -n in 'tail' that I always forget!

NO POINTS PLEASE

Regards!

...JRF...
Highlighted
Honored Contributor

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

# ls -1tr | awk '{x[NR]=$0} END {for(i=1;i