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

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

  3. George Mpouras <> writes:

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


    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 έγÏαψε:
    > George Mpouras <> writes:
    >
    >> seek FILE, 0, 0; print FILE 1111;
    >> seek FILE, 0, 0; print FILE 5;

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



    not truncate , but read/update
    George Mpouras, Jan 14, 2014
    #4
  5. George Mpouras <> writes:

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

    >
    > not truncate , but read/update


    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 έγÏαψε:
    > George Mpouras <> writes:
    >
    >>> If you need to truncate the file you have to do it explicitely. The
    >>> function you are looking for is called 'truncate'.

    >>
    >> not truncate , but read/update

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



    i tried , do not work seem to work at multiple iterations
    George Mpouras, Jan 14, 2014
    #6
  7. George Mpouras <> writes:
    > Στις 14/1/2014 13:32, ο/η Peter Makholm έγÏαψε:
    >> George Mpouras <> writes:
    >>
    >>>> If you need to truncate the file you have to do it explicitely. The
    >>>> function you are looking for is called 'truncate'.
    >>>
    >>> not truncate , but read/update

    >>
    >> What happens if you add a 'truncate FILE, 0;' right before printing the
    >> second number?


    [...]

    > i tried , do not work seem to work at multiple iterations


    --------
    open($fh, '>', '/tmp/out') or die("$!");

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

    system('cat', '/tmp/out');
    }
    --------

    works for me (in the sense that the output of cat is the last line
    written to the file, even if it is shorter than the second-to-last line.
    Rainer Weikusat, Jan 14, 2014
    #7
  8. Στις 14/1/2014 16:46, ο/η Rainer Weikusat έγÏαψε:
    > open($fh, '>', '/tmp/out') or die("$!");
    >
    > while ($data = <STDIN>) {
    > seek($fh, 0, 0);
    > truncate($fh, 0);
    > print $fh ($data);
    >
    > system('cat', '/tmp/out');
    > }




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

    system('cat', '/tmp/out');
    George Mpouras, Jan 14, 2014
    #8
  9. George Mpouras <> writes:
    > Στις 14/1/2014 16:46, ο/η Rainer Weikusat έγÏαψε:
    >> open($fh, '>', '/tmp/out') or die("$!");
    >>
    >> while ($data = <STDIN>) {
    >> seek($fh, 0, 0);
    >> truncate($fh, 0);
    >> print $fh ($data);
    >>
    >> system('cat', '/tmp/out');
    >> }

    >
    >
    >
    > this works but for reading the counter you need the shell command
    >
    > system('cat', '/tmp/out');


    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 έγÏαψε:
    > George Mpouras <> writes:
    >> Στις 14/1/2014 16:46, ο/η Rainer Weikusat έγÏαψε:
    >>> open($fh, '>', '/tmp/out') or die("$!");
    >>>
    >>> while ($data = <STDIN>) {
    >>> seek($fh, 0, 0);
    >>> truncate($fh, 0);
    >>> print $fh ($data);
    >>>
    >>> system('cat', '/tmp/out');
    >>> }

    >>
    >>
    >>
    >> this works but for reading the counter you need the shell command
    >>
    >> system('cat', '/tmp/out');

    >
    > Not really. That's just what I used because it was the easiest way to
    > display content of the file.
    >



    really
    George Mpouras, Jan 14, 2014
    #10
  11. George Mpouras <> writes:
    > Στις 14/1/2014 17:17, ο/η Rainer Weikusat έγÏαψε:
    >> George Mpouras <> writes:
    >>> Στις 14/1/2014 16:46, ο/η Rainer Weikusat έγÏαψε:
    >>>> open($fh, '>', '/tmp/out') or die("$!");
    >>>>
    >>>> while ($data = <STDIN>) {
    >>>> seek($fh, 0, 0);
    >>>> truncate($fh, 0);
    >>>> print $fh ($data);
    >>>>
    >>>> system('cat', '/tmp/out');
    >>>> }
    >>>
    >>>
    >>>
    >>> this works but for reading the counter you need the shell command
    >>>
    >>> system('cat', '/tmp/out');

    >>
    >> Not really. That's just what I used because it was the easiest way to
    >> display content of the file.
    >>

    > really


    ------
    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 έγÏαψε:
    > 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>;
    > }
    > ------


    $fh can not read at start the stored value ...
    George Mpouras, Jan 14, 2014
    #12
  13. George Mpouras <> writes:
    > Στις 14/1/2014 18:02, ο/η Rainer Weikusat έγÏαψε:
    >> 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>;
    >> }
    >> ------

    >
    > $fh can not read at start the stored value ...


    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 έγÏαψε:
    > George Mpouras <> writes:
    >> Στις 14/1/2014 18:02, ο/η Rainer Weikusat έγÏαψε:
    >>> 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>;
    >>> }
    >>> ------

    >>
    >> $fh can not read at start the stored value ...

    >
    > There is no 'stored value at start' in this example because creating the
    > file with +> truncates it to zero.
    >


    I know, that is why I end up at the solution without the truncate
    George Mpouras, Jan 14, 2014
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RGF2aWQ=?=

    Diable Explorer Button or Purge History List

    =?Utf-8?B?RGF2aWQ=?=, Feb 19, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    369
    =?Utf-8?B?RGF2aWQ=?=
    Feb 19, 2004
  2. purge like utility in c

    , Oct 10, 2006, in forum: C Programming
    Replies:
    118
    Views:
    1,626
    Dave Thompson
    Oct 23, 2006
  3. Signal

    File Read Cache - How to purge?

    Signal, Aug 21, 2007, in forum: Python
    Replies:
    14
    Views:
    493
    Hrvoje Niksic
    Aug 22, 2007
  4. how to purge backup files

    , May 10, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    180
    Joe Smith
    May 11, 2005
  5. afrinspray
    Replies:
    0
    Views:
    104
    afrinspray
    May 9, 2006
Loading...

Share This Page