Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

Flush a file keeping last 10 lines

SOLVED
Go to Solution
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
Acclaimed Contributor [Founder]

Re: Flush a file keeping last 10 lines

HI Den:

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

Regards!

...JRF...
Acclaimed Contributor [Founder]

Re: Flush a file keeping last 10 lines

tail -10 file > new-file; mv new-file file
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
Honored Contributor [Founder]

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.
Honored Contributor [Founder]

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"
Acclaimed Contributor [Founder]

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
Acclaimed Contributor [Founder]

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...
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...
Highlighted
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.
Regular Advisor [Founder]

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.