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

I want to take a Solaris pkg file and look thru it for lines which are probably human readable, with my heuristic for readability being that the first character on the line match the regexp:
/^[A-Za-z]/

How would I write a nifty one-liner to do this:, e.g.

cat gcc-295.pkg | perl_commmand_line_options_and_program_which_only_pr +ints_readable_lines

Replies are listed 'Best First'.
Re: [perlrun/perlre] one-liner to match "readable" lines
by Enlil (Parson) on Oct 07, 2002 at 17:32 UTC
    I know this is not exactly what you are asking for, but if you have a "strings" command try that. It is probably what you are looking for rather than hoping that the first character on the line is a character in the set A-Za-z. A string like:

    1. This is a line

    would not match for instance.

    -Enlil

Re: [perlrun/perlre] one-liner to match "readable" lines
by Chady (Priest) on Oct 07, 2002 at 17:15 UTC

    am I missing something?

    cat gcc-295.pkg | perl -ne 'print if /^[A-Za-z]/'
    hmm?
    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/
      To save a few keystrokes, eliminate cat:
      perl -ne 'print if /^[A-Za-z]/' gcc-295.pkg
      -Mark
Re: [perlrun/perlre] one-liner to match "readable" lines
by Abigail-II (Bishop) on Oct 07, 2002 at 17:55 UTC
    perl -pe'$_=""if!/^[A-Za-z]/' gcc-295.pkg

    But I would use the strings command as indicated by others.

    Abigail

Re: [perlrun/perlre] one-liner to match "readable" lines
by Aristotle (Chancellor) on Oct 07, 2002 at 18:19 UTC
    That is a useless use of cat. And so long as that's all you want to do, you're better off using grep(1) for this. grep '^[[:alpha:]]' gcc-295.pkg If for some reason you need to do it with Perl, I propose the -n variant already mentioned, perl -ne'print if /^[[:alpha:]]/' gcc-295.pkg as it's both easier to read and remember as well as slightly more efficient than -p in this case.

    Makeshifts last the longest.

Re: [perlrun/perlre] one-liner to match "readable" lines
by sauoq (Abbot) on Oct 07, 2002 at 21:53 UTC

    The variation of this sort of thing that I tend to use:

    perl -ne'/^[A-Za-z]/&&print'

    Besides being completely straight forward, that beats Abigail's by one stroke... :-) I do, however, agree with the others that have suggested grep(1) or strings(1). In particular, grep(1) seems to make much more sense in this particular case.

    -sauoq
    "My two cents aren't worth a dime.";
    
      As long as you're golfing, how about:
      perl -pe'$_ x=/^[A-Z]/i'
      Update: trimmed a couple more chars...

      -Blake