Professional Web Applications Themes

delete all lines in a file save it come out - PERL Beginners

Hi , I have a problem in deleting all the lines in a file and saving it . Actually my log file keep appending all the messages for that i need to clean it up i.e delete all lines in it save it . when i do this initially file shows zero bytes , but as soon as the next message appends ,, file sizes jumps to the actual size and not from zero . I tried this with flock() option also . for Somereasons it is not working I could see some junk characters like this in the file ...

  1. #1

    Default delete all lines in a file save it come out

    Hi ,
    I have a problem in deleting all the lines in a file and saving it .
    Actually my log file keep appending all the messages for that i need to
    clean it up i.e delete all lines in it save it . when i do this initially
    file shows zero bytes , but as soon as the next message appends ,, file
    sizes jumps to the actual size and not from zero . I tried this with
    flock() option also . for Somereasons it is not working I could see some
    junk characters like this in the file

    ^^^^^^^^^^^
    ^^^^^^^^^^^
    ^^^^^^^^^^^
    ^^^^^^^^^^^ Because of these characters, file size will not go to zero bytes.
    ^^^^^^^^^^^
    ^^^^^^^^^^^
    ^^^^^^^^^^^

    here is my script... Can somebody help on this ..

    for my $logfile (filelist) {
    $lines = 0;
    open(FILE, "> $logfile") or die "Couldn't open $logfile : $!\n"; # This logfile keeps appending in a linux m/c
    flock(FILE,2);
    while (sysread FILE, $buffer, 4096) {
    $lines += ($buffer =~ tr/\n//);
    }
    print FILE Sudhakar;
    flock(FILE,8);
    close(FILE);
    print "No. of lines in $logfile", $lines, "\n";
    # system "vi $logfile +delete$lines +wq"; # Delete all the lines in the file ... This command will never work for me. Dont know
    }


    Sudhakar Gajjala




    Sudhakar Guest

  2. #2

    Default RE: delete all lines in a file save it come out



    -----Original Message-----
    From: com [mailto:com]
    Sent: Tuesday, August 31, 2004 6:50 AM
    To: org
    Subject: delete all lines in a file save it come out

    Hi ,
    I have a problem in deleting all the lines in a file and saving it .
    Actually my log file keep appending all the messages for that i need to
    clean it up i.e delete all lines in it save it . when i do this
    initially
    file shows zero bytes , but as soon as the next message appends ,, file
    sizes jumps to the actual size and not from zero . I tried this with
    flock() option also . for Somereasons it is not working I could see some
    junk characters like this in the file

    ^^^^^^^^^^^
    ^^^^^^^^^^^
    ^^^^^^^^^^^
    ^^^^^^^^^^^ Because of these characters, file size will not
    go to zero bytes.
    ^^^^^^^^^^^
    ^^^^^^^^^^^
    ^^^^^^^^^^^

    here is my script... Can somebody help on this ..

    for my $logfile (filelist) {
    $lines = 0;
    open(FILE, "> $logfile") or die "Couldn't open $logfile : $!\n";
    # This logfile keeps appending in a linux m/c
    flock(FILE,2);
    while (sysread FILE, $buffer, 4096) {
    $lines += ($buffer =~ tr/\n//);
    }
    print FILE Sudhakar;
    flock(FILE,8);
    close(FILE);
    print "No. of lines in $logfile", $lines, "\n";
    # system "vi $logfile +delete$lines +wq"; # Delete all the lines
    in the file ... This command will never work for me. Dont know
    }


    Sudhakar Gajjala


    Well if all you want to do is count the number of lines in the file then
    zero out the file, the easiest way that I can think of would be to use a
    couple of system calls like this.

    $logfile = myfile.txt;

    $numlines = system(`cat $logfile |wc -l`)|| die "Cannot get number of
    lines\n";

    print $numlines;

    system(`cp /dev/null $logfile`) || die "Cannot zero load file\n";

    Of course since you seem to have a list of files you will have to modify
    this, but the general idea is there.

    And of course this script is dependent on your machine being a unix box,
    which you didn't mention what kind of OS you were running.


    Christopher Guest

  3. #3

    Default RE: delete all lines in a file save it come out

    On Aug 31, Jim said:
     

    I think you want "+>>" there, or else it will overwrite the contents of
    the file and you won't be able to determine how many lines there were
    originally.
     

    For safety's sake, use Fcntl's flock() constants.

    --
    Jeff "japhy" Pinyan % How can we ever be the sold short or
    RPI Acacia Brother #734 % the cheated, we who for every service
    http://japhy.perlmonk.org/ % have long ago been overpaid?
    http://www.perlmonks.org/ % -- Meister Eckhart

    Jeff Guest

Similar Threads

  1. Delete pages and save doent
    By Uwe_Beutler-Bussmann@adobeforums.com in forum Adobe Acrobat Macintosh
    Replies: 2
    Last Post: December 30th, 04:37 AM
  2. Can't delete .LCK files, so can't modify or save anychanges!
    By semi star gazer in forum Macromedia Dynamic HTML
    Replies: 0
    Last Post: October 23rd, 04:27 PM
  3. Unable to Save/Save As/close Word XP file with Acrobat 5.0 installed
    By Keith_Luk@adobeforums.com in forum Adobe Acrobat Windows
    Replies: 4
    Last Post: April 12th, 01:33 PM
  4. read lines of file without parsing the lines
    By W. Paulisse in forum PHP Development
    Replies: 3
    Last Post: February 11th, 01:25 AM
  5. Delete multiple blank lines
    By Wiggins D Anconia in forum PERL Beginners
    Replies: 3
    Last Post: January 20th, 07:36 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139