Grep notes

From DiLab
Jump to: navigation, search

Simple things first: search for a pattern recursively in all files, ignore the case, and display the line number in the files where found:

grep -rin 'mypattern' *


Sometimes you do not want to find certain things - use '-v' for that:

grep -v 'mypattern' *


My favorite alias for grep - excludes */.svn/* files and only looks in certain file types. The find command goes through the directories and prepares a list of file names, then xargs them into egrep. Note, if you have file names starting with '-', you may have a problem using this.

alias mygrep='find . | grep -v "\.\/svn\/" | egrep "\.(c|cpp|h|java|html|rb|rhtml|css)$" | xargs egrep -n'


Note that grep and egrep differ, for example, that in grep you can not ask for match this or that, for example, "(aaa|bbb)", but in egrep you can.


Nice and concise article on grep and regex usage: http://www.wellho.net/regex/grep.html