Loading ...
Sorry, an error occurred while loading the content.
 

Re: [PBML] search & replace a string in text file.

Expand Messages
  • zsdc
    ... Why not? Because it would completely corrupt this data: 123|Name|Subject|Read|Receipt 124|Name2|Subject2|Read|Receipt 12507|Mr. Readan O Ready|Reading the
    Message 1 of 10 , Feb 29, 2004
      Jonas wrote:
      > begin :
      >
      >>Rob Dowell wrote:
      >>
      >>>Untested, but I would try:
      >>>
      >>>s/Read/Unread/ if ($_ =~ /125/);
      >>
      >>It would work correctly on the data set Sara has posted as an example,
      >>but would silently corrupt the data if used on, e.g.:
      >>
      >>123|Name|Subject|Read|Receipt
      >>124|Name2|Subject2|Read|Receipt
      >>125|Name3|Subject3|Read|Receipt
      >>126|Name4|Subject4|Read|Receipt
      >>247|Name5|Reading Subject 999125999|Read|Receipt
      >>
      >>so be _very_ careful with that.
      >
      > ...
      >
      > True, but why not use
      >
      > s/Read/Unread/ if $_=~/^125/;
      >
      > ?

      Why not? Because it would completely corrupt this data:

      123|Name|Subject|Read|Receipt
      124|Name2|Subject2|Read|Receipt
      12507|Mr. Readan O'Ready|Reading the reports|Read|Receipt
      12508|Mr. Readan O'Ready|Read it already|Read|Receipt
      12599|Name5|Ready to publish the reports|Read|Receipt

      What I meant was, why not use the code I posted few minuted earlier:

      #!/usr/bin/perl

      use strict;
      use warnings;
      use AnyData;

      my $file = 'data.txt';
      my $cols = 'id,name,subject,status,receipt';

      my $table = adTie 'Pipe', $file, 'u', {cols => $cols};

      $table->{{id => '125'}} = {status => 'Unread'};

      __END__

      Of course you don't _have_ to use AnyData::Format::Pipe to update pipe
      delimited file, just like you don't have to use CGI.pm to parse CGI
      GET/POST parameters, but then you have to reinvent its functionality.

      So, if you really want to do it in a one-liner, then you have to use
      something like this:

      perl '-paF\|' -i~ '-e$"="|";$F[3]="Unread"if$F[0]==125;$_="@F"' file

      --
      ZSDC
    Your message has been successfully submitted and would be delivered to recipients shortly.