purge line

Discussion in 'Perl Misc' started by George Mpouras, Jan 14, 2014.

  1. I want my line to be 5 not 5111



    my $counter;
    open FILE, '+<:raw', 'test.txt' or die "$!\n";

    seek FILE, 0, 0;
    read ( FILE , $counter , 64 );
    print $counter;

    seek FILE, 0, 0; print FILE 1111;
    seek FILE, 0, 0; print FILE 5;

    close FILE;
     
    George Mpouras, Jan 14, 2014
    #1
    1. Advertisements

  2. # this seems to work but probably there is something better

    my $counter;
    open FILE, '+<:raw', 'test.txt' or die "$^E\n";

    seek FILE, 0, 0;
    read ( FILE , $counter , 70 );
    $counter=~tr/\x20//sd;

    print "*$counter*";

    $counter = 1111; seek FILE, 0, 0; printf FILE "%64d", $counter;
    $counter = 5 ; seek FILE, 0, 0; printf FILE "%64d", $counter;
    close FILE;
     
    George Mpouras, Jan 14, 2014
    #2
    1. Advertisements

  3. If you need to truncate the file you have to do it explicitely. The
    function you are looking for is called 'truncate'.

    //Makholm
     
    Peter Makholm, Jan 14, 2014
    #3
  4. Στις 14/1/2014 11:35, ο/η Peter Makholm έγÏαψε:

    not truncate , but read/update
     
    George Mpouras, Jan 14, 2014
    #4
  5. What happens if you add a 'truncate FILE, 0;' right before printing the
    second number?

    For me it seems to do as you require. Of course this assumes that you
    only have a single value in the file and don't need to update a value in
    the middle of a larger file.

    If you have multiple values in the file and need to update a single
    value you would either need to rewrite all values following the value
    you need to change og used a fixed format where the values have a fixed
    length.

    //Makholm
     
    Peter Makholm, Jan 14, 2014
    #5
  6. Στις 14/1/2014 13:32, ο/η Peter Makholm έγÏαψε:

    i tried , do not work seem to work at multiple iterations
     
    George Mpouras, Jan 14, 2014
    #6
  7. --------
    open($fh, '>', '/tmp/out') or die("$!");

    while ($data = <STDIN>) {
    seek($fh, 0, 0);
    truncate($fh, 0);
    print $fh ($data);

    system('cat', '/tmp/out');
    }
     
    Rainer Weikusat, Jan 14, 2014
    #7
  8. Στις 14/1/2014 16:46, ο/η Rainer Weikusat έγÏαψε:


    this works but for reading the counter you need the shell command

    system('cat', '/tmp/out');
     
    George Mpouras, Jan 14, 2014
    #8
  9. Not really. That's just what I used because it was the easiest way to
    display content of the file.
     
    Rainer Weikusat, Jan 14, 2014
    #9
  10. Στις 14/1/2014 17:17, ο/η Rainer Weikusat έγÏαψε:

    really
     
    George Mpouras, Jan 14, 2014
    #10
  11. ------
    open($fh, '+>', '/tmp/out') or die("$!");

    while ($data = <STDIN>) {
    seek($fh, 0, 0);
    truncate($fh, 0);
    print $fh ($data);

    print("content of out\n");
    seek($fh, 0, 0);
    printf("\t%s", $_) for <$fh>;
    }
    ------
     
    Rainer Weikusat, Jan 14, 2014
    #11
  12. Στις 14/1/2014 18:02, ο/η Rainer Weikusat έγÏαψε:
    $fh can not read at start the stored value ...
     
    George Mpouras, Jan 14, 2014
    #12
  13. There is no 'stored value at start' in this example because creating the
    file with +> truncates it to zero.
     
    Rainer Weikusat, Jan 14, 2014
    #13
  14. Στις 14/1/2014 19:46, ο/η Rainer Weikusat έγÏαψε:
    I know, that is why I end up at the solution without the truncate
     
    George Mpouras, Jan 14, 2014
    #14
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.