in reply to Re: Count of patterns in a file without knowing pattern in advance
in thread Count of patterns in a file without knowing pattern in advance

Thanks to both for the solution. I have that working now.
  • Comment on Re^2: Count of patterns in a file without knowing pattern in advance

Replies are listed 'Best First'.
Re^3: Count of patterns in a file without knowing pattern in advance
by NetWallah (Canon) on Jun 11, 2019 at 03:06 UTC
    There is usually a "one liner" alternative for simple requirements.

    I offer:

    perl -anE "$h{$F[0]}++}{say qq|$_ appeared $h{$_}| for sort keys %h" +*YOUR-FILE*
    Use single quotes if you are on Linux.

                    Time is an illusion perpetrated by the manufacturers of space.

      Unfortunately, this experimental feature no longer exists in perl version 5.24.1 and forward...produces the following error message:

      "Experimental keys on scalar is now forbidden at -e line 1."

      "It's not how hard you work, it's how much you get done."

        Odd - works fine for me:

        $ cat foo one two one two three one four $ perl -anE '$h{$F[0]}++}{say qq|$_ appeared $h{$_}| for sort keys %h' + foo four appeared 1 one appeared 3 three appeared 1 two appeared 2 $ perl -v | head -3 This is perl 5, version 26, subversion 1 (v5.26.1) built for x86_64-li +nux-gnu-thread-multi (with 62 registered patches, see perl -V for more detail) $

        Are you sure you substituted single quotes as NetWallah advised?

        perl -anE "$h{$F[0]}++}{say qq|$_ appeared $h{$_}| for sort keys %h" *YOUR-FILE*
        Unfortunately, this experimental feature no longer exists in perl version 5.24.1 and forward...produces the following error message: "Experimental keys on scalar is now forbidden at -e line 1."

        Are you sure? I don't see that feature being used in this script, did you perhaps make a typo when copying it? (Update: Note the $h vs. %h in the final example below.)

        $ perl -v This is perl 5, version 28, subversion 1 (v5.28.1) built for x86_64-li +nux ... $ cat foo.txt one two three one five two ten one $ perl -anE '$h{$F[0]}++}{say qq|$_ appeared $h{$_}| for sort keys %h' + foo.txt five appeared 1 one appeared 3 ten appeared 1 three appeared 1 two appeared 2 $ perl -anE '$h{$F[0]}++}{say qq|$_ appeared $h{$_}| for sort keys $h' + foo.txt Experimental keys on scalar is now forbidden at -e line 1. Type of arg 1 to keys must be hash or array (not scalar dereference) a +t -e line 1, at EOF Execution of -e aborted due to compilation errors.