Skip to content

Instantly share code, notes, and snippets.

@mmcgraw73
Forked from isyufu/grep.sh
Last active April 10, 2019 13:12
Show Gist options
  • Save mmcgraw73/fccb2cbd56943d78a04a0615c0e57257 to your computer and use it in GitHub Desktop.
Save mmcgraw73/fccb2cbd56943d78a04a0615c0e57257 to your computer and use it in GitHub Desktop.
grep cheat sheet

GENERAL

print lines begining with range of letters

grep ^[A-D] table.txt

REGEX

search for word which has any single character followed by ello

grep ".ello" demo.txt

OR, all the below examples produce the same results

grep "stuff|more" demo.txt grep -E "stuff|more" demo.txt grep -e "stuff" -e "more" demo.txt

AND, there is no AND so regex works

grep 'Manager.*Sales' employee.txt

AND & OR

grep 'Manager.Sales|DeveloperTech' employee.txt

begin with 1 and endswith C

grep '^1.*C$' file.txt

FLAGS

print everything that doesn't contain the query

grep -v ^Da* table.txt

-i forgets about case sensitivity

grep -i ^DA table.txt

show only the matched string, not the entire line in which it is present within.

grep -o "Meow" table.txt

print the file containing the query including thos within subdirs

grep -r ^David ~/scripts-x14.04/bash/*

print the name of the file(s) which matches the query

grep -l ^David ~/some_dir/*

count the number of matches

grep -c "stuff" table.txt

show the line numbers of the matches

grep -n "go" demo.txt

search only for the full word

grep -w "of" table.txt

print 3 lines after the match (-B for before the match & -C for before and after)

grep -A 3 "of" table.txt

print the number of occurrences + file name that string appears

grep -RIci "string here" . | awk -v FS=":" -v OFS="\t" '$2>0 { print $2, $1 }' | sort -hr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment