🤖
Guides
  • Introduction
  • Beginners
    • Getting Started
  • Guides
    • SQLi Walkthrough
    • My First BoF
    • OSCP Buffer Overflow Guide (Windows)
    • Parrot OS customisation
    • Terminal Customisation
    • Video Guides
  • Cheat Sheets
    • Reverse Shells
    • Tunnelling, Pivoting and Proxies
    • SQL Injection
      • WAF Bypass
      • SQLMap
      • DBMS Cheatsheets
        • MSSQL
        • MySQL
        • Oracle
        • SQLite
        • PostgreSQL
      • References
    • Bash Cheat Sheets
      • Terminal
      • Find
      • Grep
      • Sed
      • Awk
      • Xargs
      • System
      • Download
      • Networking
      • Hardware
      • Variable
      • Math
      • Data Manipulation
      • Random
      • Time
      • Condition and Loop
      • Other
    • OSINT
    • Ping Sweeps
  • Methodologies
    • VOIP Checklist
    • OWASP v4 Checklist
    • External Inf
    • Internal Infrastructure
  • Linux
    • Checklist - Linux Priv Esc
  • Windows
    • Checklist - Windows Priv Esc
  • Things to do/look at
Powered by GitBook
On this page
  • Grep
  • Type of grep
  • Grep and count number of empty lines
  • Grep and return only integer
  • Grep integer with certain number of digits (e.g. 3)
  • Grep only IP address
  • Grep whole word (e.g. 'target')
  • Grep returning lines before and after match (e.g. 'bbo')
  • Grep string starting with (e.g. 'S')
  • Extract text between words (e.g. w1,w2)
  • Grep lines without word (e.g. 'bbo')
  • Grep lines not begin with string (e.g. #)
  • Grep variables with space within it (e.g. myvar="some strings")
  • Grep only one/first match (e.g. 'bbo')
  • Grep and return number of matching line(e.g. 'bbo')
  • Count occurrence (e.g. three times a line count three times)
  • Case insensitive grep (e.g. 'bbo'/'BBO'/'Bbo')
  • COLOR the match (e.g. 'bbo')!
  • Grep search all files in a directory(e.g. 'bbo')
  • Search all files in directory, do not ouput the filenames (e.g. 'bbo')
  • Search all files in directory, output ONLY the filenames with matches(e.g. 'bbo')
  • Grep OR (e.g. A or B or C or D)
  • Grep AND (e.g. A and B)
  • Regex any single character (e.g. ACB or AEB)
  • Regex with or without a certain character (e.g. color or colour)
  • Grep all content of a fileA from fileB
  • Grep a tab
  • Grep variable from variable
  • Grep strings between a bracket()
  • Grep number of characters with known strings in between(e.g. AAEL000001-RA)
  • Skip directory (e.g. 'bbo')

Was this helpful?

  1. Cheat Sheets
  2. Bash Cheat Sheets

Grep

Grep

Type of grep

grep = grep -G # Basic Regular Expression (BRE)
fgrep = grep -F # fixed text, ignoring meta-charachetrs
egrep = grep -E # Extended Regular Expression (ERE)
pgrep = grep -P # Perl Compatible Regular Expressions (PCRE)
rgrep = grep -r # recursive

Grep and count number of empty lines

grep -c "^$"

Grep and return only integer

grep -o '[0-9]*'
#or
grep -oP '\d'

Grep integer with certain number of digits (e.g. 3)

grep ‘[0-9]\{3\}’
# or
grep -E ‘[0-9]{3}’
# or
grep -P ‘\d{3}’

Grep only IP address

grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
# or
grep -Po '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'

Grep whole word (e.g. 'target')

grep -w 'target'

#or using RE
grep '\btarget\b'

Grep returning lines before and after match (e.g. 'bbo')

# return also 3 lines after match
grep -A 3 'bbo'

# return also 3 lines before match
grep -B 3 'bbo'

# return also 3 lines before and after match
grep -C 3 'bbo'

Grep string starting with (e.g. 'S')

grep -o 'S.*'

Extract text between words (e.g. w1,w2)

grep -o -P '(?<=w1).*(?=w2)'

Grep lines without word (e.g. 'bbo')

grep -v bbo filename

Grep lines not begin with string (e.g. #)

grep -v '^#' file.txt

Grep variables with space within it (e.g. myvar="some strings")

grep "$myvar" filename
#remember to quote the variable!

Grep only one/first match (e.g. 'bbo')

grep -m 1 bbo filename

Grep and return number of matching line(e.g. 'bbo')

grep -c bbo filename

Count occurrence (e.g. three times a line count three times)

grep -o bbo filename |wc -l

Case insensitive grep (e.g. 'bbo'/'BBO'/'Bbo')

grep -i "bbo" filename

COLOR the match (e.g. 'bbo')!

grep --color bbo filename

Grep search all files in a directory(e.g. 'bbo')

grep -R bbo /path/to/directory
# or
grep -r bbo /path/to/directory

Search all files in directory, do not ouput the filenames (e.g. 'bbo')

grep -rh bbo /path/to/directory

Search all files in directory, output ONLY the filenames with matches(e.g. 'bbo')

grep -rl bbo /path/to/directory

Grep OR (e.g. A or B or C or D)

grep 'A\|B\|C\|D'

Grep AND (e.g. A and B)

grep 'A.*B'

Regex any single character (e.g. ACB or AEB)

grep 'A.B'

Regex with or without a certain character (e.g. color or colour)

grep ‘colou?r’

Grep all content of a fileA from fileB

grep -f fileA fileB

Grep a tab

grep $'\t'

Grep variable from variable

$echo "$long_str"|grep -q "$short_str"
if [ $? -eq 0 ]; then echo 'found'; fi
#grep -q will output 0 if match found
#remember to add space between []!

Grep strings between a bracket()

grep -oP '\(\K[^\)]+'

Grep number of characters with known strings in between(e.g. AAEL000001-RA)

grep -o -w "\w\{10\}\-R\w\{1\}"
# \w word character [0-9a-zA-Z_] \W not word character

Skip directory (e.g. 'bbo')

grep -d skip 'bbo' /path/to/files/*
PreviousFindNextSed

Last updated 4 years ago

Was this helpful?