Skip to content

Instantly share code, notes, and snippets.

@d4rkr00t
Last active November 8, 2018 13:48
Show Gist options
  • Save d4rkr00t/b55743800bc4597c9050 to your computer and use it in GitHub Desktop.
Save d4rkr00t/b55743800bc4597c9050 to your computer and use it in GitHub Desktop.
Grep command snippets

Simple GREP

grep CPU: /var/run/dmesg.boot

Count grepped lines

grep WARNING /var/run/dmesg.boot -c

Search by entire word

grep -w 'seven' test.txt

Search by word start or end

grep '\<seven' test.txt

grep 'seven\>' test.txt

Search by start or end of line

grep '^seven' test.txt

grep 'seven$' test.txt

Show nearby lines of found

grep -C 1 twentyseven test.txt

grep -A 1 twentyseven test.txt # onle above

grep -B 1 twentyseven test.txt # only below

By regular expr

grep "twenty[1-4]" test.txt

grep "twenty[^1-4]" test.txt

grep -E '\b[0-9]{1,3}(\.[0-9]{1,3}){3}\b' /etc/resolv.conf  #ip

grep -oE '\b[0-9]{1,3}(\.[0-9]{1,3}){3}\b' /etc/resolv.conf

Exclude

grep '_blank' -r ./libs --exclude-dir="libs/mooa" --exclude="*.json"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment