Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

Flush a file keeping last 10 lines

SOLVED
Go to solution
Leo The Cat
Regular Advisor

Flush a file keeping last 10 lines

Hi Guys

Is there a way to flush a big text file but keeping last 10 lines without use any other temporary file ?

Bests regards
Den
10 REPLIES
James R. Ferguson
Acclaimed Contributor

Re: Flush a file keeping last 10 lines

HI Den:

# $ perl -e '@old=reverse <>;@new=(@old)[0..9];print reverse @new' file

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Flush a file keeping last 10 lines

tail -10 file > new-file; mv new-file file
Leo The Cat
Regular Advisor

Re: Flush a file keeping last 10 lines

Hi

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
Patrick Wallek
Honored Contributor
Solution

Re: Flush a file keeping last 10 lines

Well, that would work, but you said in your initial post:

>>without use any other temporary file ?

Your solution is using a temp file.
Highlighted
Peter Nikitka
Honored Contributor

Re: Flush a file keeping last 10 lines

Hi,

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



The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Dennis Handly
Acclaimed Contributor

Re: Flush a file keeping last 10 lines

>Peter: There is no advantage to append a file to an empty file and destroy it afterwards.

One possible reason is to keep existing permissions and ACLs:
cat new-file > file
rm -f new-file
James R. Ferguson
Acclaimed Contributor

Re: Flush a file keeping last 10 lines

Hi (again) Den:

> ...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...
Leo The Cat
Regular Advisor

Re: Flush a file keeping last 10 lines

It's true with my solution I missed the fact that I can't use any Temporary file (sic). sorry...
Leo The Cat
Regular Advisor

Re: Flush a file keeping last 10 lines

The JRF's solution is probably the best. it works and do the trick without this story of temporary file.

Thanks James !
Bests Regards
Den.
Mike Miller_8
Regular Advisor

Re: Flush a file keeping last 10 lines

tail -10 showopt.rpt | {^Jwhile read linea^Jdo^Jecho $linea^Jdone^J}

show sweet and to the point.