data67 has asked for the wisdom of the Perl Monks concerning the following question:

Here is the problem:

I have a data-file like so:

1..., some words, Dec 13, 2001 2..., some words, Jan 02, 2002

What i want to do is to be able to is go through each line and be able to search the first field. which is my identifier and if condition matches, append "somethng" to the end of that same line not new line.

Example: search for question 1.

1..., some words, Dec 13, 2001 2..., some words, Jan 02, 2002
if true?
append ,P to end of that line.

-------------------- Thanks!!!!!!

Replies are listed 'Best First'.
Re: Regex that will append to end of same line
by rbc (Curate) on Jan 04, 2002 at 01:44 UTC
    Oops ... my original post was intended to
    be used with sed, or vi style regex.

    In perl you could do ...

    while(<>) { s/(^1.*)/\1,P/g; print; }


    Sorry about the confusion


    --
    Its like a dog that can sing and dance.
    It's remarkable because it can do it.
    Not that it can do it well.

      In Perl, we use $1 instead of \1 in the substitution string. The right hand side is just another interpolated string.

      From perlop:

      It is at this step that "\1" is begrudgingly converted to $1 in the replacement text of "s///" to correct the incorrigible sed hackers who haven't picked up the saner idiom yet. A warning is emitted if the "use warnings" pragma or the -w command-line flag (that is, the $^W variable) was set.

      From Jeffrey E. F. Friedl's great book Mastering Regular Expressions:

      The Perl manpage makes a concerted effort to point out that \1 is not available as a backreerence outside of a regex. (Use the variable $1 instead.) \1 is much more than a simple notational convenience - the variable $1 refers to a string of static text matched during some previously completed successful match. On the other hand, \1 is a true regex metacharacter to match text similar to that matched within the first parenthesized subexpression (...)

      perl -we's//\1/' 2>&1 |splain

      \1 better written as $1 at -e line 1 (#1)
      (W syntax) Outside of patterns, backreferences live on as variables. The use of backslashes is grandfathered on the right-hand side of a substitution, but stylistically it's better to use the variable form because other Perl programmers will expect it, and it works better if there are more than 9 backreferences.

      And dot star (.*) is bad in this case.

      2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

        Thanks!

        I knew I'd learn something from Perlmonks!

        --
        Its like a dog that can sing and dance.
        It's remarkable because it can do it.
        Not that it can do it well.
Re: Regex that will append to end of same line
by rbc (Curate) on Jan 04, 2002 at 00:55 UTC
    Do you mean something like ...

    s/^1\(.*\)/1\1,P/g;

    --
    Its like a dog that can sing and dance.
    It's remarkable because it can do it.
    Not that it can do it well.
      Would this work:

      s/^1/^$,P/g

       

      Can you calrify yours please. Thanks..
Re: Regex that will append to end of same line
by data67 (Monk) on Jan 04, 2002 at 01:32 UTC
    I got it:

    my $text = "1..., some words, Dec 13, 2001"; if ($text =~ m/^\1/) { $text =~ m/(.*$)/; print "${1},P"; }

    Thanks ;-}

      Nooooo! :) See Death to dot star to read why .* is bad.
      You can't use \1 there, you should just use 1.
      Please note that /^1/ will also match strings beginning with 123.

      I suggest the following solution:
      $line =~ s/$/,P/ if /^1\D/;


      Which will substitute the end of the line (which is _before_ a newline if there is any) with ,P (and thus "append" it) if the line starts with 1 followed by anything-that-is-not-a-digit.

      2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

      Or, even shorter (and hopefully clearer) ..
      my $text = "1..., some words, Dec 13, 2001"; print "$text,P" if ( $text =~ /^1/ );
      Although you may want \n at the end of the line.

      --t. alex

      "Excellent. Release the hounds." -- Monty Burns.