grep
/ egrep
/ pgrep
/ fgrep
| Cheatsheet
Basic grep to match HTTP URLs
grep -Eo 'http://[^ ]+' filename
Basic grep to match HTTPS URLs
grep -Eo 'https://[^ ]+' filename
Basic grep to match FTP URLs:
grep -Eo 'ftp://[^ ]+' filename
Grep with case-insensitive matching for HTTP or HTTPS URLs:
grep -Eio 'https?://[^ ]+' filename
Grep with word boundaries to match HTTP, HTTPS, or FTP URLs:
grep -Eo '\b(https?|ftp)://[^ ]+\b' filename
Grep to match URLs starting with HTTP, HTTPS, or FTP:
grep -Eo '^(http|https|ftp)://[^ ]+' filename
Grep with line numbers for HTTP or HTTPS URLs:
grep -Eno 'https?://[^ ]+' filename
Grep with inverted match to exclude HTTP, HTTPS, or FTP URLs:
grep -vE '(http|https|ftp)://[^ ]+' filename
Grep to match URLs containing specific domain names (example: google.com):
grep -Eo 'https?://[^ ]+google\.com[^ ]*' filename
Grep to match URLs with query parameters (example: ?key=value):
grep -Eo 'https?://[^ ]+\?[^ ]+' filename
Basic grep to match HTTP URLs within href attributes:
grep -Eo 'href="http://[^"]+' filename
Basic grep to match HTTPS URLs within href attributes:
grep -Eo 'href="https://[^"]+' filename
Basic grep to match FTP URLs within href attributes:
grep -Eo 'href="ftp://[^"]+' filename
Grep with case-insensitive matching for HTTP or HTTPS URLs within href attributes:
grep -Eio 'href="https?://[^"]+' filename
Grep with word boundaries to match HTTP, HTTPS, or FTP URLs within href attributes:
grep -Eo 'href="\b(https?|ftp)://[^"]+\b' filename
Grep to match URLs starting with HTTP, HTTPS, or FTP within href attributes:
grep -Eo 'href="^(http|https|ftp)://[^"]+' filename
Grep with line numbers for HTTP or HTTPS URLs within href attributes:
grep -Eno 'href="https?://[^"]+' filename
Grep with inverted match to exclude HTTP, HTTPS, or FTP URLs within href attributes:
grep -vE 'href="(http|https|ftp)://[^"]+' filename
Grep to match URLs containing specific domain names within href attributes (example: google.com):
grep -Eo 'href="https?://[^"]+google\.com[^"]*' filename
Grep to match URLs with query parameters within href attributes (example: ?key=value):
grep -Eo 'href="https?://[^"]+\?[^"]+' filename
Credit Card Matching
Amex Card
grep -Eo '^3[47][0-9]{13}$' filename
BCGlobal
grep -Eo '^(6541|6556)[0-9]{12}$' filename
Carte Blanche Card
grep -Eo '^389[0-9]{11}$' filename
Diners Club Card
grep -Eo '^3(?:0[0-5]|[68][0-9])[0-9]{11}$' filename
Discover Card
grep -Eo '^65[4-9][0-9]{13}|64[4-9][0-9]{13}|6011[0-9]{12}|(622(?:12[6-9]|1[3-9][0-9]|[2-8][0-9][0-9]|9[01][0-9]|92[0-5])[0-9]{10})$' filename
Insta Payment Card
grep -Eo '^63[7-9][0-9]{13}$' filename
JCB Card
grep -Eo '^(?:2131|1800|35\d{3})\d{11}$' filename
KoreanLocalCard
grep -Eo '^9[0-9]{15}$' filename
Laser Card
grep -Eo '^(6304|6706|6709|6771)[0-9]{12,15}$' filename
Maestro Card
grep -Eo '^(5018|5020|5038|6304|6759|6761|6763)[0-9]{8,15}$' filename
Mastercard
grep -Eo '^(5[1-5][0-9]{14}|2(22[1-9][0-9]{12}|2[3-9][0-9]{13}|[3-6][0-9]{14}|7[0-1][0-9]{13}|720[0-9]{12}))$' filename
Solo Card
grep -Eo '^(6334|6767)[0-9]{12}|(6334|6767)[0-9]{14}|(6334|6767)[0-9]{15}$' filename
Switch Card
grep -Eo '^(4903|4905|4911|4936|6333|6759)[0-9]{12}|(4903|4905|4911|4936|6333|6759)[0-9]{14}|(4903|4905|4911|4936|6333|6759)[0-9]{15}|564182[0-9]{10}|564182[0-9]{12}|564182[0-9]{13}|633110[0-9]{10}|633110[0-9]{12}|633110[0-9]{13}$' filename
Union Pay Card
grep -Eo '^(62[0-9]{14,17})$' filename
Visa Card
grep -Eo '^4[0-9]{12}(?:[0-9]{3})?$' filename
Visa Master Card
grep -Eo '^(?:4[0-9]{12}(?:[0-
9]{3})?|5[1-5][0-9]{14})$' filename
IP Address Matching
Basic grep to match IPv4 addresses:
grep -Eo '\b([0-9]{1,3}\.){3}[0-9]{1,3}\b' filename
Grep to match IPv4 addresses with octet ranges (0-255):
grep -Eo '\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b' filename
Grep to match IPv4 addresses with CIDR notation:
grep -Eo '\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}/([0-9]|[1-2][0-9]|3[0-2])\b' filename
Basic grep to match IPv6 addresses:
grep -Eo '\b([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}\b' filename
Grep to match IPv6 addresses with full notation (including zeros):
grep -Eo '\b([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}\b' filename
Grep to match IPv6 addresses with collapsed notation (compressing consecutive zeros):
grep -Eo '\b(([0-9a-fA-F]{1,4}:){1,6}|::([0-9a-fA-F]{1,4}:){0,5}|([0-9a-fA-F]{1,4})?::([0-9a-fA-F]{1,4}:){0,4}|([0-9a-fA-F]{1,4}(:|$)){1,7})\b' filename
Social Security Number Matching
grep -Eo '\b[0-9]{8}-[0-9]{4}\b' filename
grep -Eo '\b[0-9]{6}-[0-9]{4}\b' filename
grep -Eo '\b[0-9]{3}-[0-9]{3}-[0-9]{3} [0-9]{2}\b' filename
grep -Eo '\b[0-9]{10}\b' filename
grep -Eo '\b[0-9]{3}-[0-9]{2}-[0
-9]{4}\b' filename
grep -Eo '\b[0-9]{9}\b' filename
Vehicle Registration Number Matching
grep -Eo '\b[A-Z]{3}-[A-Z]{3}\b' filename
grep -Eo '\b[A-Z]\s[0-9]{3}\s[A-Z]{2}\s[0-9]{2}\b' filename
grep -Eo '\b[A-Z]{3}\s[0-9]{4}\b' filename
Phone Number Matching
General regular expression pattern for phone numbers:
\b(?:\+?\d{1,3}[\s-]?)?(?:\()?[\d\s-]{8,}\)?\b
Regular expression to match phone numbers in Sweden:
\b(?:\+46|0)[\s-]?([1-9][0-9]{1,2})[\s-]?(\d{2})[\s-]?(\d{2})[\s-]?(\d{2})\b
Regular expression to match phone numbers in the United States:
\b(?:\+?1[\s-]?)?(?:\()?[\d\s-]{10}\)?\b
Regular expression to match phone numbers in the United Kingdom:
\b(?:\+?44[\s-]?)?(?:\()?[\d\s-]{10,12}\)?\b
Regular expression to match phone numbers in Canada:
\b(?:\+?1[\s-]?)?(?:\()?[\d\s-]{10}\)?\b
Regular expression to match phone numbers in Australia:
\b(?:\+?61[\s-]?)?(?:\()?[\d\s-]{8,12}\)?\b
Regular expression to match phone numbers in Germany:
\b(?:\+?49[\s-]?)?(?:\()?[\d\s-]{9,12}\)?\b
Regular expression to match phone numbers in France:
\b(?:\+?33[\s-]?)?(?:\()?[\d\s-]{9,12}\)?\b
MAC Address Matching
grep -Eo '\b([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})\b' filename
IMSI Number Matching
grep -Eo '\b\d{3}\d{2}-\d{6,10}\b' filename
IMEI Number Matching
grep -Eo '\b\d{2}-\d{6}-\d{6}-\d\b' filename
Scene Release Matching
grep -Eo '\b[A-Za-z0-9]+(\.[A-Za-z0-9]+){2}\b' filename
Timestamp and Datestamp Matching
grep -Eo '\b([01]\d|2[0-3]):([0-5]\d):([0-5]\d)\b' filename
grep -Eo '\b\d{4}-\d{2}-\d{2}\b' filename
Logging Patterns in dmesg
Grep to match error messages in dmesg
logs:
grep -Ei 'error' filename
Grep to match normal messages in dmesg
logs:
grep -Ei 'normal' filename
Grep to match info messages in dmesg
logs:
Grep to match debugging messages in dmesg
logs:
grep -Ei 'debug' filename