- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- ksh script Q about removing entries from a list in...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-30-2008 05:55 AM
тАО01-30-2008 05:55 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-30-2008 06:00 AM
- Tags:
- tail
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-30-2008 06:02 AM
тАО01-30-2008 06:02 AM
Re: ksh script Q about removing entries from a list in a VAR
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-30-2008 06:37 AM
тАО01-30-2008 06:37 AM
Re: ksh script Q about removing entries from a list in a VAR
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-30-2008 07:04 AM
тАО01-30-2008 07:04 AM
Re: ksh script Q about removing entries from a list in a VAR
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-30-2008 07:07 AM
тАО01-30-2008 07:07 AM
Re: ksh script Q about removing entries from a list in a VAR
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-30-2008 07:13 AM
тАО01-30-2008 07:13 AM
Re: ksh script Q about removing entries from a list in a VAR
It was :
MYLIST=`ls -1t logs.* | tail -n +3`
Eric
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-30-2008 07:36 AM
тАО01-30-2008 07:36 AM
Re: ksh script Q about removing entries from a list in a VAR
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
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-30-2008 07:47 AM
тАО01-30-2008 07:47 AM
Re: ksh script Q about removing entries from a list in a VAR
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-30-2008 07:49 AM
тАО01-30-2008 07:49 AM
Re: ksh script Q about removing entries from a list in a VAR
- Tags:
- awk