Last match

Discussion in 'Perl Misc' started by Caj Zell, Apr 26, 2006.

  1. Caj Zell

    Caj Zell Guest

    Quite often a colleague asks me if I can help him traverse a file
    resulting from debug output and pick out numeric values from some
    pattern, comma-separated. For example, he may be interested in the
    variable var1 and I tell him to do a little something like this:

    perl -n -e 'if(/var1 = (\d+)/){print $1.",\n"};' debug.txt > out.txt

    and pick out the last comma.

    He's quite allright by this, but I wondered if there is any easy way to
    avoid that last comma?
    Caj Zell, Apr 26, 2006
    #1
    1. Advertising

  2. Caj Zell

    Dr.Ruud Guest

    Caj Zell schreef:


    > perl -n -e 'if(/var1 = (\d+)/){print $1.",\n"};' debug.txt > out.txt
    > there is any easy way to avoid that last comma?


    1. Push the matches in an array, and print the stringized array with
    ",\n" as separator.

    perl -nle '/var1 = (\d+)/ and push @_, $1 }{ $"=",\n"; print "@_"'

    2. Concatenate the matches as ",$1\n", and use substr($s,1).

    perl -ne '/var1 = (\d+)/ and $s. = ",$1\n" }{ print substr $s, 1'

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Apr 26, 2006
    #2
    1. Advertising

  3. Caj Zell

    Dr.Ruud Guest

    Dr.Ruud schreef:

    > perl -ne '/var1 = (\d+)/ and $s. = ",$1\n" }{ print substr $s, 1'


    The ". =" was meant as " .=".

    Variant:

    perl -ne '$s .= ",$1\n" if /var1 = (\d+)/ }{ print substr $s, 1'


    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Apr 26, 2006
    #3
  4. Caj Zell

    Guest

    Caj Zell wrote:
    > Quite often a colleague asks me if I can help him traverse a file
    > resulting from debug output and pick out numeric values from some
    > pattern, comma-separated. For example, he may be interested in the
    > variable var1 and I tell him to do a little something like this:
    >
    > perl -n -e 'if(/var1 = (\d+)/){print $1.",\n"};' debug.txt > out.txt
    >
    > and pick out the last comma.
    >
    > He's quite allright by this, but I wondered if there is any easy way to
    > avoid that last comma?



    If you have enough memory to slurp the file, then consider:

    $ perl -l012 -0777 -ne 'print join ",\n" => /var1 = (\d+)/g;' filename

    See 'perldoc perlrun' for details.

    --
    Hope this helps,
    Steven
    , Apr 26, 2006
    #4
  5. Caj Zell

    Caj Zell Guest

    Thanks!

    You guys are really incredible!
    Caj Zell, Apr 26, 2006
    #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. hiwa
    Replies:
    0
    Views:
    636
  2. Victor
    Replies:
    2
    Views:
    638
    Victor
    May 17, 2004
  3. ekzept
    Replies:
    0
    Views:
    365
    ekzept
    Aug 10, 2007
  4. John Gordon
    Replies:
    13
    Views:
    476
    Ian Kelly
    Dec 20, 2011
  5. Volkan Civelek

    Match doesn't match

    Volkan Civelek, Jul 19, 2006, in forum: Ruby
    Replies:
    4
    Views:
    163
Loading...

Share This Page