Hero search

By Filip Salomonsson; published on May 15, 2007.

Have you ever hero searched? Let's have a look at some grep options.

  • -E makes grep interpret the search pattern as an extended regular expression (equivalent to running egrep, but in this case, going for the option instead makes the acronym prettier);
  • -R makes it go through directories recursively;
  • -h suppresses the filename prefix in the output when searching multiple files;
  • -o makes it output only the matching substring, not the whole line that contained it.

Whack'em together, and you get -ERho. Or, well, -hoRE. No, wait. -hERo! I dub thee "hero search" (mmm, mnemonics)!

Case in point: quick and dirty statistics of character entities in a directory of 500 sgml files:

$ grep -hERo '&[^;]+;' /path/to/files | sort | uniq -c | sort -nr
     64 č
     30 ř
     24 ě