Re: Deleting all the data after a specific position in a text file.
by BrowserUk (Patriarch) on Nov 18, 2011 at 11:01 UTC
|
Is there a better way than using truncate ?
What problem do you have with using truncate?
It is a single call and quite efficient.
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [Watch: Dir/Any] |
|
The problem is that we have to calculate the file size and then subtract to get the value of the truncate LENGTH.
| [reply] [Watch: Dir/Any] |
|
You does not have to. If you mean size in bytes rather than characters, this can work:
seek $FH,-100, 1 or die $!;
truncate "file", tell $FH or die $!; # Cannot use $FH in truncate, it'
+s closed
| [reply] [Watch: Dir/Any] [d/l] |
|
c:\test>dir junk.dat
18/11/2011 19:23 893 junk.dat
c:\test>perl -E"$f=pop; $s=-s($f); truncate $f, $s-40 or die $!" junk.
+dat
c:\test>dir junk.dat
18/11/2011 19:24 853 junk.dat
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [Watch: Dir/Any] [d/l] |
Re: Deleting all the data after a specific position in a text file.
by afoken (Chancellor) on Nov 18, 2011 at 11:02 UTC
|
Is there a better way than using truncate ?
Define "better". Faster? Less code? Data wiped according to some military standard?
Alexander
--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
| [reply] [Watch: Dir/Any] |
Re: Deleting all the data after a specific position in a text file.
by choroba (Cardinal) on Nov 18, 2011 at 11:02 UTC
|
Better in what respect? What is the format of the data? How do you find the position? Where in the data is the position located (close to the beginning or end...)? | [reply] [Watch: Dir/Any] |
|
The file pointer is present at the EOF and I want to remove the last 100 characters.
| [reply] [Watch: Dir/Any] |
|
See the documentation for seek, specifically the WHENCE parameter.
Update: I am assuming that you mean the last 100 bytes, not characters.
Update 2: Specifically, seek to position 100 from the end, and tell what the current position is, then truncate to that length. ISTR (but cannot recall at the moment) a syscall to "truncate from this point to the end of the file". Oops, just use the FH version of truncate. See also Re^3: Deleting all the data after a specific position in a text file..
| [reply] [Watch: Dir/Any] [d/l] |
Re: Deleting all the data after a specific position in a text file.
by ansh batra (Friar) on Nov 18, 2011 at 12:06 UTC
|
#! usr/bin/perl -w
open(FILE,"<file1.txt");
my @lines=<FILE>;
close(FILE);
open(FILE,">file1.txt");
foreach $line(@lines)
{
if($line =~ /.*keywordhere.*/)
{
close(FILE);
exit;
}
else
{
print FILE "$line";
}
}
close(FILE);
deffinately not better than truncate
was looking for something like this??? | [reply] [Watch: Dir/Any] [d/l] |
Re: Deleting all the data after a specific position in a text file.
by sundialsvc4 (Abbot) on Nov 18, 2011 at 15:13 UTC
|
Once you have opened the file (exclusively, and for read/write access), the operating system will instantly tell you how large it is and will allow you to seek to any position within it ... relative to the beginning, or to the end, or to wherever you are now. The filesystem will also provide the capability to truncate the file, and it’s the only way to accomplish that feat, i.e. truncate.
Get the file size, and if it is longer than 300 bytes (heh...), position 300 bytes from the end and truncate. Mission Accomplished.
| [reply] [Watch: Dir/Any] |