Simple egrep pattern

I'm new to egrep. What pattern could I use to find all lines that match this pattern: <beginning of line><any amount of whitespace>sub<space>. I want it to return the entire line.

(I'm trying to generate a list of all Perl sub definitions in a list of Perl modules.)

Thanks for your help!

First, it's suggested to use grep -E instead of egrep, since it's more portable.
Second, pretty much the same pattern that you would use in Perl.

  1. Portability isn't a concern to me since I'm not going to deploy this command.

  2. What pattern would I use in Perl then?

can you post sample input you want to grep

I have a collection of Perl code files which define several subs. Most appear to have some whitespace (spaces or tabs) before the first line of the sub definition:

sub subname {

I want to use egrep (or grep -E) to find all these, because simple grep for the word sub returns lots of lines I don't want to see (like Perl comments and things in quotes).

Examples of lines I don't want to see:
# sub subname {
print "This sub does something"

Portability doesn't have anything to do with deployment IMO, but should be looked after by the developer.
And the pattern you're looking for is

^[[:space:]]*sub[[:space:]]+ #grep -E
^\s*sub\s+ #perl

say you don't want lines with "# sub subname " and want only "sub subname {" to be listed.

grep -v "# sub subname" .perl | grep -h "sub subname {"
grep -v "# sub subname" ./
| grep -h "sub subname {"

Ah there's the answer I was looking for. Here's the command I used to make a list of all subs defined in all files, for anyone who's interested:

grep -e ^[[:space:]]*sub[[:space:]] * > sublist.txt

As far as Amit's post goes, his commands could have been condensed to the following, and would not have worked regardless because I wasn't searching for the exact string "subname." (I thought that would have been obvious.)

grep "sub subname {" * | grep -v "#"