- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Flush a file keeping last 10 lines
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
тАО05-09-2011 10:05 AM
тАО05-09-2011 10:05 AM
Is there a way to flush a big text file but keeping last 10 lines without use any other temporary file ?
Bests regards
Den
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-09-2011 10:20 AM
тАО05-09-2011 10:20 AM
Re: Flush a file keeping last 10 lines
# $ perl -e '@old=reverse <>;@new=(@old)[0..9];print reverse @new' file
Regards!
...JRF...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-09-2011 11:21 AM
тАО05-09-2011 11:21 AM
Re: Flush a file keeping last 10 lines
- Tags:
- tail
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-09-2011 11:37 AM
тАО05-09-2011 11:37 AM
Re: Flush a file keeping last 10 lines
I have tried the Perl idea, with a small adjust for my case it works. However I have prefered this solution.
1.
tail -n 10 $i > /tmp/odb_Local_OracleCRSlogMngt.tmp.$DATE
2.
> $i
and finally
3.
cat /tmp/odb_Local_OracleCRSlogMngt.tmp.$DATE >> $i
rm -f /tmp/odb_Local_OracleCRSlogMngt.tmp.$DATE
What do you think about that guys ?
Bests Regards
Den
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-09-2011 11:47 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-09-2011 11:50 AM
тАО05-09-2011 11:50 AM
Re: Flush a file keeping last 10 lines
I see only one difference to Dennis' solution: you put a 'speaking name' for the temporary file. So:
Keep 1.
Drop 2.
There is no need to open an empty file because ...
Change 3.
There is no advantage to append a file to an empty file and destroy it afterwards. Just use
mv /tmp/odb_Local_OracleCRSlogMngt.tmp.$DATE $i
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-09-2011 12:07 PM
тАО05-09-2011 12:07 PM
Re: Flush a file keeping last 10 lines
One possible reason is to keep existing permissions and ACLs:
cat new-file > file
rm -f new-file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-09-2011 12:22 PM
тАО05-09-2011 12:22 PM
Re: Flush a file keeping last 10 lines
> ...without [the] use of any other temporary file
Well, I guess I didn't provide that; only a method to meet the criteria of the last lines. So, try:
# ./cat keep10
#!/usr/bin/perl
use strict;
use warnings;
use Fcntl;
my $file = shift or die;
open my $fh, '<', $file or die "Can't open '$file':$!\n";
my @old=reverse <$fh>;
my @new=(@old)[0..9];
close $fh;
open $fh, '+>', $file or die "Can't open '$file':$!\n";
print {$fh} reverse @new;
1;
...run as:
# ./keep10 file
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-09-2011 12:27 PM
тАО05-09-2011 12:27 PM
Re: Flush a file keeping last 10 lines
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-09-2011 12:28 PM
тАО05-09-2011 12:28 PM
Re: Flush a file keeping last 10 lines
Thanks James !
Bests Regards
Den.