Grep Mastery: URLs, Credit Cards, and IPs
Discover the art of efficient pattern matching using grep for URLs, credit cards, and IP addresses with detailed examples and use cases.
URL Matching
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 to match Swedish social security numbers (12 characters, format: yyyymmdd-xxxx):
grep -Eo '\b[0-9]{8}-[0-9]{4}\b' filename
Grep to match Swedish social security numbers (10 characters, format: yymmdd-xxxx):
grep -Eo '\b[0-9]{6}-[0-9]{4}\b' filename
Grep to match Russian social security numbers (format: xxx-xxx-xxx xx):
grep -Eo '\b[0-9]{3}-[0-9]{3}-[0-9]{3} [0-9]{2}\b' filename
Grep to match Russian social security numbers (format: xxxxxxxxx):
grep -Eo '\b[0-9]{10}\b' filename
Grep to match USA social security numbers (format: xxx-xx-xxxx):
grep -Eo '\b[0-9]{3}-[0-9]{2}-[0
-9]{4}\b' filename
Grep to match USA social security numbers (format: xxxxxxxxx):
grep -Eo '\b[0-9]{9}\b' filename
Vehicle Registration Number Matching
Grep to match Swedish vehicle registration numbers (format: ABC-123):
grep -Eo '\b[A-Z]{3}-[A-Z]{3}\b' filename
Grep to match Russian vehicle registration numbers (format: A 999 AA 77):
grep -Eo '\b[A-Z]\s[0-9]{3}\s[A-Z]{2}\s[0-9]{2}\b' filename
Grep to match United States vehicle registration numbers (format: AAA 9999):
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 to match MAC addresses in the format XX:XX:XX:XX:XX:XX:
grep -Eo '\b([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})\b' filename
IMSI Number Matching
Grep to match IMSI numbers (International Mobile Subscriber Identity) in the format MCCMNC-MSIN:
grep -Eo '\b\d{3}\d{2}-\d{6,10}\b' filename
IMEI Number Matching
Grep to match IMEI numbers (International Mobile Equipment Identity) in the format AA-BBBBBB-CCCCCC-D:
grep -Eo '\b\d{2}-\d{6}-\d{6}-\d\b' filename
Scene Release Matching
Grep to match scene release names in the format GROUP.TITLE.RELEASE:
grep -Eo '\b[A-Za-z0-9]+(\.[A-Za-z0-9]+){2}\b' filename
Timestamp and Datestamp Matching
Grep to match timestamps in the format HH:MM:SS:
grep -Eo '\b([01]\d|2[0-3]):([0-5]\d):([0-5]\d)\b' filename
Grep to match datestamps in the format YYYY-MM-DD:
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 -Ei 'info' filename
Grep to match debugging messages in dmesg
logs:
grep -Ei 'debug' filename