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

RE: [PBML] Search/Replace in File (Discuss)

Expand Messages
  • Fortuno, Adam
    Not that I have some grand experience with Perl to be joining this discussion, but I always like to express my opinion about things I know nothing about. To
    Message 1 of 2 , Mar 1 7:30 AM
      Not that I have some grand experience with Perl to be joining this
      discussion, but I always like to express my opinion about things I know
      nothing about.

      To Jonas:

      # Regarding your suggestion
      s/Read/Unread/ if $_=~/^125/;

      # My revision (I hate extra typing, even a little)
      s/Read/Unread/ if (/^125/);

      To ZSDC:

      You're point is well noted. The logic in Jonas' statement gives way to
      problems down the line. In my newbish way, I would've simply coded it a
      little different.

      #! usr/bin/perl

      # The delimiter to use.
      my($delim) = '|';

      # Break the file into an array, locate the specified line,
      # and replace the specified value.
      while (<>) {
      chomp;
      my(@line) = split(/$delim/);
      $_[3] = 'un-read' if ("\L$_[0]" == "\Lread");
      print (join($delim, @line));
      }

      However, one problem I have is a I get really bizzar (to me) results. The
      result of the line `my(@line) = split(/$delim/);` give me an array where
      each element gets 1-char (e.g. 123|Name|Subject|Read|Receipt equals
      1|2|3|N|a|m|e|...). That certainly wasn't what I expected, and I'm not sure
      why. Any ideas?

      Regards,
      Adam

      -----Original Message-----
      From: zsdc [mailto:zsdc@...]
      Sent: Sunday, February 29, 2004 10:12 PM
      To: perl-beginner@yahoogroups.com
      Subject: Re: [PBML] search & replace a string in text file.


      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
    • Paul Archer
      ... The vertical bar | is the alternation character in regular expressions. So your pattern of /|/ means match on nothing or nothing . You need to escape
      Message 2 of 2 , Mar 1 8:11 AM
        10:30am, Fortuno, Adam wrote:

        > #! usr/bin/perl
        >
        > # The delimiter to use.
        > my($delim) = '|';
        >
        > # Break the file into an array, locate the specified line,
        > # and replace the specified value.
        > while (<>) {
        > chomp;
        > my(@line) = split(/$delim/);
        > $_[3] = 'un-read' if ("\L$_[0]" == "\Lread");
        > print (join($delim, @line));
        > }
        >
        > However, one problem I have is a I get really bizzar (to me) results. The
        > result of the line `my(@line) = split(/$delim/);` give me an array where
        > each element gets 1-char (e.g. 123|Name|Subject|Read|Receipt equals
        > 1|2|3|N|a|m|e|...). That certainly wasn't what I expected, and I'm not sure
        > why. Any ideas?
        >
        The vertical bar '|' is the alternation character in regular expressions. So
        your pattern of /|/ means match on 'nothing' or 'nothing'. You need to
        escape the bar (put a backslash in front of it) to tell Perl to treat it as
        a literal.

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