command perl - SR

Discussion in 'Perl Misc' started by fred, Sep 3, 2009.

  1. fred

    fred Guest

    Would like to replace my third field (empty) for each five lines by
    foo. This command is not correct. Can you help me fix it ?

    !perl -pe 's(&&)($n++ = 5 ? (&foo&)eg' text.txt

    xxxx&(ght)(hgf)&&(yyt)
    xx9x&(gg)(ff)&&(yyt)
    oixxx&(hfd)(jj)&&(yyt)
    xxxx&(jj)(kk)&&(yyt)
    xjhxxx&(jj)(j)&&(yyt)


    Thanks
    fred, Sep 3, 2009
    #1
    1. Advertising

  2. fred

    Tim McDaniel Guest

    In article <>,
    fred <> wrote:
    >Would like to replace my third field (empty) for each five lines by
    >foo. This command is not correct. Can you help me fix it ?
    >
    >!perl -pe 's(&&)($n++ = 5 ? (&foo&)eg' text.txt


    I'm afraid that your syntax and semantics have enough wrong that it
    would take a while to explain.

    I think that approach is barking up the wrong tree anyway. It looks
    more like array element manipulation than string processing. I think
    it's easier to use split(), either explicitly or implicitly, to split
    the input into an array, and then just manipulate $field[2].

    You didn't explicitly specify what to do
    - if the third field is not empty.
    I gather that you would want it to be left alone.
    - if there is no third field.
    I assume that no third field is to be added.
    and the test data didn't include it such test cases.

    I also include a test case to make sure that the right number of empty
    fields (other than the third) are preserved.

    perl '-F&' -wape 'use strict;
    if (@F > 2 && $F[2] eq "") {
    $F[2] = "foo"; $_ = join("&", @F);
    }' 093.txt

    applied to

    xxxx&(ght)(hgf)&&(yyt)
    xx9x&(gg)(ff)&&(yyt)
    oixxx&(hfd)(jj)&&(yyt)
    xxxx&(jj)(kk)&&(yyt)
    xjhxxx&(jj)(j)&&(yyt)
    xjhxxx&(jj)(j)&NO FOO HERE&(yyt)
    only_one_field
    two&fields
    &null&&fields&5 more&&&&&

    produces

    xxxx&(ght)(hgf)&foo&(yyt)
    xx9x&(gg)(ff)&foo&(yyt)
    oixxx&(hfd)(jj)&foo&(yyt)
    xxxx&(jj)(kk)&foo&(yyt)
    xjhxxx&(jj)(j)&foo&(yyt)
    xjhxxx&(jj)(j)&NO FOO HERE&(yyt)
    only_one_field
    two&fields
    &null&foo&fields&5 more&&&&&


    (I don't know a variable to use instead of the repeated "&".)

    --
    Tim McDaniel,
    Tim McDaniel, Sep 3, 2009
    #2
    1. Advertising

  3. fred

    Guest

    On Thu, 3 Sep 2009 10:55:18 -0700 (PDT), fred <> wrote:

    >Would like to replace my third field (empty) for each five lines by
    >foo. This command is not correct. Can you help me fix it ?
    >
    >!perl -pe 's(&&)($n++ = 5 ? (&foo&)eg' text.txt
    >
    >xxxx&(ght)(hgf)&&(yyt)
    >xx9x&(gg)(ff)&&(yyt)
    >oixxx&(hfd)(jj)&&(yyt)
    >xxxx&(jj)(kk)&&(yyt)
    >xjhxxx&(jj)(j)&&(yyt)
    >
    >
    >Thanks


    perl -pe 's/^(.+&.+&)(&.+)$/$1foo$2/' text.txt
    or
    perl -pe 's/^((?:.+&){2})(&.+)$/$1foo$2/' text.txt

    -sln
    , Sep 3, 2009
    #3
  4. fred

    Guest

    On Thu, 3 Sep 2009 10:55:18 -0700 (PDT), fred <> wrote:

    >Would like to replace my third field (empty) for each five lines by
    >foo. This command is not correct. Can you help me fix it ?
    >
    >!perl -pe 's(&&)($n++ = 5 ? (&foo&)eg' text.txt
    >
    >xxxx&(ght)(hgf)&&(yyt)
    >xx9x&(gg)(ff)&&(yyt)
    >oixxx&(hfd)(jj)&&(yyt)
    >xxxx&(jj)(kk)&&(yyt)
    >xjhxxx&(jj)(j)&&(yyt)
    >
    >
    >Thanks


    If only the first 5 lines:

    perl -0 -pe 's/^(.+&.+&)(&.+)/++$n<6 ? $1.foo.$2 : $1.$2/mge' text.txt
    or
    perl -0 -pe 's/^((?:.+&){2})(&.+)/++$n<6 ? $1.foo.$2 : $1.$2/mge' text.txt

    -sln
    , Sep 3, 2009
    #4
  5. fred

    Guest

    On Thu, 3 Sep 2009 10:55:18 -0700 (PDT), fred <> wrote:

    >Would like to replace my third field (empty) for each five lines by
    >foo. This command is not correct. Can you help me fix it ?
    >
    >!perl -pe 's(&&)($n++ = 5 ? (&foo&)eg' text.txt
    >
    >xxxx&(ght)(hgf)&&(yyt)
    >xx9x&(gg)(ff)&&(yyt)
    >oixxx&(hfd)(jj)&&(yyt)
    >xxxx&(jj)(kk)&&(yyt)
    >xjhxxx&(jj)(j)&&(yyt)
    >
    >
    >Thanks


    Or, to fix what you have (untested):

    perl -0 -pe 's/(&&)/++$n < 6 ? "&foo&" : $1/eg' text.txt
    or
    perl -0 -pe 's(&&)(++$n < 6 ? "&foo&" : "&&")eg' text.txt

    -sln
    , Sep 3, 2009
    #5
    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. dpackwood
    Replies:
    3
    Views:
    1,773
  2. lucy
    Replies:
    7
    Views:
    179
    Joe Smith
    Sep 3, 2004
  3. Replies:
    2
    Views:
    153
    Tad McClellan
    Mar 26, 2005
  4. PerlFAQ Server

    FAQ 1.4 What are Perl 4, Perl 5, or Perl 6?

    PerlFAQ Server, Jan 23, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    288
    PerlFAQ Server
    Jan 23, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    658
    PerlFAQ Server
    Feb 3, 2011
Loading...

Share This Page