k-man has asked for the wisdom of the Perl Monks concerning the following question:

I'm getting a count of lines matching a pattern.

This code works:

ACTION_PATTERN='perl$' export ACTION_PATTERN ROWS=$(perl -n -e '$count++ if /$ENV{ACTION_PATTERN}/; END { print $co +unt }' ~/.zsh_history) echo "ROWS:${ROWS}"

Is there a simpler alternative to exporting?

Replies are listed 'Best First'.
Re: Calling perl from zsh w/ vars
by hippo (Archbishop) on Jul 18, 2020 at 11:33 UTC

    Not a Perl solution, but you can simply unquote the var:

    ACTION_PATTERN='perl$' ROWS=$(perl -n -e '$count++ if /'$ACTION_PATTERN'/; END { print $count + }' ~/.zsh_history) echo "ROWS:${ROWS}"

    Caveat: only tested in bash. I'd be surprised if it didn't work in zsh too, though.

      Indeed, thanks

Re: Calling perl from zsh w/ vars
by AnomalousMonk (Archbishop) on Jul 18, 2020 at 15:15 UTC

    I'd prefer Marshall's approach of using a grep-like utility if you have one, but if not:

    c:\@Work\Perl\monks>grep -c "xms;$" elide_lines.pl.bak 2 c:\@Work\Perl\monks>perl -ne "BEGIN { $AP = shift } $n++ if /$AP/; END + { print $n }" "xms;$" elide_lines.pl.bak 2


    Give a man a fish:  <%-{-{-{-<

Re: Calling perl from zsh w/ vars
by Fletch (Bishop) on Jul 20, 2020 at 18:46 UTC

    Most Borne-style shells (of which zsh, bash, and ksh are some) will let you define an environment variable for a single command by prefixing the command itself with something like:

    ACTION_PATTERN='perl$' perl -ne '$count++ if m{$ENV{ACTION_PATTERN}};END{print $count, qq{\n};}' ~/.zsh_history.

    You could do that here, but I'm wondering if there's not a bit of an XY problem in that you're wanting to use an arbitrary pattern in your search command and aren't sure how to pass that as an argument into your oneliner. If that were the case you could always do something like this and use shift to pull it out of @ARGV before the -n option starts iterating over things:

    ROWS=$(perl -nE 'BEGIN{$PATTERN=shift();$PATTERN=qr/$PATTERN/;};$count +++ if m{$PATTERN};}{ say $count' 'perl$' ~/.zsh_history) echo "ROWS:$ROWS"

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Calling perl from zsh w/ vars
by Marshall (Canon) on Jul 18, 2020 at 11:44 UTC
    I had to look for what .zsh meant. I guess this is a Mac/Apple thing.
    Searching for a count of matching pattern lines, normally uses grep, there is an option to print the number of lines matched.
    Perl is not required. However Perl can be used for that without requiring a shell program or grep. Why not use command line grep for "count of lines matching a pattern"?
      «...a Mac/Apple thing»

      He, yes and no 🤪😎 They replaced bash with zsh. See also. Best regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

      perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

        I like your link to Z Shell Wiki. My Unix shell experience is mainly with BASH.
      I guess this is a Mac/Apple thing.

      No.