Grep non-greedy match

2020-07-24

By default grep doesn’t support non-greedy modifiers, you need to use the perl syntax: grep -P.

greedy match

echo "<html><p>This</p><p>is</p><p>some</p><p>content</p></html>" | grep -E "<p>.+</p>" -o

<p>This</p><p>is</p><p>some</p><p>content</p>
non-greedy match

echo "<html><p>This</p><p>is</p><p>some</p><p>content</p></html>" | grep -P "<p>.+?</p>" -o

<p>This</p>
<p>is</p>
<p>some</p>
<p>content</p>
Snippetsgrepregex

Postgres CSV export

jq replace json value if key exists