1 / 15

Grep Examples

Grep Examples. Constructing Greps. Find Social Security Numbers in network traffic (if your a crook:) 466 - 90 - 1234 only this number ddd - dd - dddd any SS# with dashes. Constructing Greps. ddd -? dd -? dddd any SS# with or without dashes

dyami
Download Presentation

Grep Examples

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Grep Examples

  2. Constructing Greps • Find Social Security Numbers in network traffic (if your a crook:) • 466-90-1234 • only this number • \d\d\d-\d\d-\d\d\d\d • any SS# with dashes

  3. Constructing Greps • \d\d\d-?\d\d-?\d\d\d\d • any SS# with or without dashes • \d\d\d[\W]?\d\d[\W]?\d\d\d\d • any SS# with or without non-alphanumeric • \d{3}[\W]?\d{2}[\W]?\d{4}

  4. Example 2 • Find all Jons • Jon • every capitalized “Jon” • J[oO][nN] • JON,JoN,JOn

  5. Example 2 • ^J[Oo][Nn] • Lines staring with JON, Jon, JOn, JoN • ^J[Oo][Nn]\s • last one AND a space • (don’t want to find Jonathan)

  6. Example 2 • ^J[Oo][Hh]?[Nn]\s • optional H or h adds: • JOHN , JoHn , JOHn , JoHN • JOhN , John , JOhn , JohN • Notice how using if() comparisons quickly becomes a TON of work?

  7. Back References • Refer to sub-strings after the engine has run • the FSM engine remembers more than just the resulting state of a search (true/false) • Really REALLY useful

  8. Back References • () grouping • \1 to \9 or $1 to $9 (depends on environment) • each pair of () encloses text you want to refer back to • From left to right each () gets a number

  9. Example • Replace “fred” with “Fred” but could also work with “freddie” (just 1 input) • f(redd?i?e?) • Replace with: • F$1 or F\1 (depending on environment)

  10. Example • Some places you can use back references in your search pattern! • Finding the word “at” if it was typed 2 times: at at • (at)\s\1 • Saves copy/paste of the (at) for an easier to read regexp

  11. Advanced Example • Some places you can specify a quantity on a back reference! • Finding multiple number codes: • 12–12–45–89–35– • 56– • (\d\d–)+

  12. Back References • Other special references: • $` (backtick: the key to the left of 1) • everything before the match • $’ (single quote or apostrophe) • everything after the match

  13. Back References • $+ • whatever char the last [] matched • $& • the entire string again-- useless • NOTE: Back references can be slow depending on the engine used

  14. OR • The | (pipe) is used like a logical OR • Combine multiple patterns into 1 • Sometimes faster than 2+ separate searches of the text

  15. Example OR • Find: • f(red)|f(rank) • Replaced with: • "F$1" (or “F\1”) • Capitalizes both fred and frank • *depends on engine if \1,\2

More Related