Notes on grep.

Usage

grep <search> <file/path>
grep "<phrase>" <file>
grep '<regexp>' <file>

most common usage

grep -inr "texttofind"

grep -ril "texttofind" *

-r : recursive
-n : show line number
-i : ignore case
-l : show only the filenames. prints only the filenames with matching text.
-v : print all lines that does NOT match the pattern

Example

$ grep chicken *
$ grep "a phrase" file  # looks for the entire phrase inside the quote.

find markdown files with images ![]()

# https://jaketrent.com/post/conversion-from-octopress-to-hugo/
grep -o -c "^\!\[.*\]\(.*\)" * | awk -F: '{if ($2 > 0){print $1}}'

Search for dotfiles / hidden files

By default, grep skips .dotfiles. To include .dotfiles,

grep <search> * .*    # * searches non-dot files, and .* searches dot files

grep -r <search> .      # this will search all files, but must use "-r" and "."

Recursive

grep -r<...> "texttofind" .  # ". can be replaced with * but cannot have any file pattern/glob"

To restrict grep by file extension type,

grep -r --include "*.txt" TextToFind .

# note: grep ... *.txt won't work!  use --include

fgrep

Same as grep -F, searches for multiple matches. No regular expression is allowed.

fgrep "<search> \n <search> \n ... " <file>

$ fgrep "chicken"

Searches for a list of words (in file “list”) at file.txt

$ fgrep -f list file.txt

Regex

egrep is same as grep -E, Most powerful grep, uses extended reg-exp

egrep <search>... <file>
  • by default, it uses simple regex. Instead, use grep -E or egrep.
  • I use alias to make it use “egrep” all the time

Example

$grep '^\.D[SE]$' text

REGULAR EXPRESSION

'.' matches any single char.
chil. = chili, chile

'*' matches 0 or more repetition of preceding character
ap*le = ale, apple

'[ ]' matches any of the characters enclosed in brackets.
[Cc]hicken = chicken, Chicken

'^' beginning of line
^Beef = Beef at beginning of line

'$' End of line
soup$ = soup at end of line

'\ ' Treat next character as literal, not regular expression
go\.to = look for "go.to", not "goxto"

Alternative

Feature comparison of ack, ag, git-grep, GNU grep and ripgrep excellent chart on difference in usage / features https://news.ycombinator.com/item?id=16096824

awk http://blog.jpalardy.com/posts/skip-grep-use-awk/ https://news.ycombinator.com/item?id=14692233

$ [data is generated] | awk '/something/'

Ag (silver searcher) is highly recommended. Ag is faster because it does better at ignoring files, while grep doesn’t ignore files well.

brew install the_silver_searcher
apt-get install silversearcher-ag

Ripgrep https://github.com/BurntSushi/ripgrep ripgrep combines the usability of The Silver Searcher with the raw speed of grep.

brew install ripgrep    # unoptimized mac version
choco install ripgrep  #win

Ack

  • grep for programmer, written all in Perl

Platinum Searcher https://github.com/monochromegane/the_platinum_searcher

  • in Go

Misc notes

Older Gnu grep was slow (pre 2.7). Workaround http://rg03.wordpress.com/2009/09/09/gnu-grep-is-slow-on-utf-8/.

For those older grep, use:

alias grep='GREP_COLOR="1;37;45" LANG=C grep --color=auto'

References