Showing posts with label grep. Show all posts
Showing posts with label grep. Show all posts

Wednesday, February 3, 2010

Grep: Yet Another Reason I Love Bash (the GNU/Linux Born Again Shell)

I have to say how incredibly awesome it is to be able to issue a one-liner like the following:

egrep -w --color --exclude-dir='.svn' '[Bb]usiness [Uu]nits?|BUs?' WEB-INF/jsp/*

Everywhere the expression Business Unit, business unit, or BU appears in the JSP pages is now listed on my screen in color.

How does it work?


egrep

Run grep (the GNU "find" utility) using Extended (that's where the E comes from) regular expressions.

-w

I was looking up the word boundary expression in the man page for grep and found that the -w switch forces the match to occur within word boundaries.

--color

Show the file names and matches in a different color (red) from the other text.

--exclude-dir='.svn'

Prevents grep from looking in subversion (source control) directories

'[Bb]usiness [Uu]nits?|BUs?'

Match "business unit" with or without initial caps and with or without an "s" at the end, or match "BU". These matches have to occur on word-boundaries (because of the -w above) so that ABUSE does not match.

WEB-INF/jsp/*

Run the match against all the files in the WEB-INF/jsp directory.




I pay about $150/year for an individual license for InteliJ IDEA because of it's refactoring, but it still can't do this. You can pry bash from my cold, dead hands.

Tuesday, February 3, 2009

New Favorite Find/Replace (bash) shell commands

These commands took a while for me to find and I now use them very, very often. One is to find files in all subdirectories excluding certain directories (in this case, the subversion ".svn" directories). The second expression replaces regular expressions in all the files that match the find in the first expression:

# To grep for things excluding subversion .svn directories

find . -path '*/.svn' -prune -o -type f -print | xargs -e fgrep -I PATTERN

# To replace the same (using sed), only in files that grep matched, only in non .svn files.
find . -path '*/.svn' -prune -o -type f -print | xargs -e fgrep -l -I 'OLD' | xargs sed -i -e 's/OLD/NEW/g'


UPDATE 2009-02-09
I was browsing at Barnes and Noble over the weekend and the O'Reilly Pocket Guide to Grep mentioned an --exclude-dir option. I looked at the man page again and it's there too. I even googled again and found it right away. How did I miss it? That makes things much easier.

# To grep for things excluding subversion .svn directories

fgrep --exclude-dir='.svn' -rI PATTERN *

# To replace the same (using sed), only in files that grep matched, only in non .svn files.
fgrep --exclude-dir='.svn' -rIl 'OLD' * | xargs sed -i -e 's/OLD/NEW/g'


UPDATE 2010-03-31
# To find whole words, using an even briefer syntax for

# chaining the output of grep into the input for sed.
# Taken from a shell script that sets oldWord and
# newWord, but you get the idea:
sed -i -e "s/\<$oldWord\>/$newWord/g" $(fgrep --exclude-dir='.svn' -wrIl "$oldWord" *)