Re: Flush a file keeping last 10 lines (301 Views)
Reply
Regular Advisor
Leo The Cat
Posts: 206
Registered: ‎06-13-2007
Message 1 of 11 (301 Views)
Accepted Solution

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
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 11 (301 Views)

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
Dennis Handly
Posts: 25,056
Registered: ‎03-06-2006
Message 3 of 11 (301 Views)

Re: Flush a file keeping last 10 lines

tail -10 file > new-file; mv new-file file
Regular Advisor
Leo The Cat
Posts: 206
Registered: ‎06-13-2007
Message 4 of 11 (301 Views)

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
Patrick Wallek
Posts: 13,772
Registered: ‎06-21-2000
Message 5 of 11 (301 Views)

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
Peter Nikitka
Posts: 1,575
Registered: ‎02-10-2003
Message 6 of 11 (301 Views)

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
Dennis Handly
Posts: 25,056
Registered: ‎03-06-2006
Message 7 of 11 (301 Views)

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
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 8 of 11 (301 Views)

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
Leo The Cat
Posts: 206
Registered: ‎06-13-2007
Message 9 of 11 (301 Views)

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...
Regular Advisor
Leo The Cat
Posts: 206
Registered: ‎06-13-2007
Message 10 of 11 (301 Views)

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
Mike Miller_8
Posts: 89
Registered: ‎02-24-2003
Message 11 of 11 (301 Views)

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.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.