Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Deleting all the data after a specific position in a text file.

by pankaj_it09 (Scribe)
on Nov 18, 2011 at 10:59 UTC ( [id://938781]=perlquestion: print w/replies, xml ) Need Help??

pankaj_it09 has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I want to delete all the data after a specific position in a file.
Is there a better way than using truncate ?

Replies are listed 'Best First'.
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.
      The problem is that we have to calculate the file size and then subtract to get the value of the truncate LENGTH.
        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
        The problem is that we have to calculate the file size and then subtract to get the value of the truncate LENGTH.

        Is that soo difficult?

        Maybe you're making the same mistake as the other posters and thinking you need to open the file first and seek around in it? You don't:

        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.
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". ;-)
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...)?
      The file pointer is present at the EOF and I want to remove the last 100 characters.

        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..

        --MidLifeXis

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???
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://938781]
Approved by BrowserUk
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-03-29 06:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found