DEV Community

Discussion on: Demystifying GREP

Collapse
 
voyeg3r profile image
SΓ©rgio AraΓΊjo • Edited

We can also use some advanced regex like in this example

any word preceded by big

grep -P '(?<=big)\s\w+' <<-EOF
big house
small house
big car
small car
EOF

outputs each line that NOT starts with big

grep -P '(?<!big)\s\w+' <<-EOF
big house
small house
big car
small car
EOF

The grep below will catch only lines that start with any word but not followed
by house

grep -P '\w+ (?!house)' <<-EOF
big house
small house
big car
small car
EOF

Now any word followed by house

grep -P '\w+ (?=house)' <<-EOF
big house
small house
big car
small car
EOF

The final example:

I had an html file with some wallpapers "name" on it, and I wanted to print just the names without full path

grep -Poh '(?<=href="/pt-br/wallpapers/details/)[^/]*' file-1.html

-P ...................... Perl regex
-o ...................... only the match
-h ...................... omit the filename
(?<=pattern) .... look behind
[^/]* ................... any character but slash

And an ASCII table about some of these regex patterns

    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚ Lookaround  β”‚ Name                 β”‚ What it Does                   β”‚
    β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
    β”‚ (?=foo)     β”‚ Lookahead            β”‚ word that follows is foo       β”‚
    β”‚ (?<=foo)    β”‚ Lookbehind           β”‚ preceding word is foo          β”‚
    β”‚ (?!foo)     β”‚ Negative Lookahead   β”‚ word that follows is NOT foo   β”‚
    β”‚ (?<!foo)    β”‚ Negative Lookbehind  β”‚ word behind is NOT foo         β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜