3 ms·
ripgrep can do it! :) rg 'foo=([0-9]+)' --replace '$1' or more succinctly: rg 'foo=([0-9]+)' -r '$1' Example: $ echo 'quux=123 foo=123 bar=123'
by burntsushi 4y ago
ripgrep can do it! :)
rg 'foo=([0-9]+)' --replace '$1'
or more succinctly:
rg 'foo=([0-9]+)' -r '$1'
Example:
$ echo 'quux=123 foo=123 bar=123' | rg 'foo=([0-9]+)' -r '$1'
quux=123 123 bar=123
Named groups work too:
$ echo 'quux=123 foo=123 bar=123' | rg 'foo=(?P<digits>[0-9]+)' -or '$digits'
123
You can also replace the whole match by combining --replace with --only-matching:
$ echo 'quux=123 foo=123 bar=123' | rg 'foo=([0-9]+)' -or '$1'
123
Of course, I understand capturing groups are useful to have in awk when you're using awk. ripgrep can only handle very simplistic cases. But they tend to be quite common.
- nirinor 4y agoI've needed these capabilities often while using awk for converting messy logs/error outputs into tables/commands. Nowadays I like the nushell approach to the composition: echo 'quux=123 foo=123 bar=123' | str replace '.*quux=([0-9]+).*foo=([0-9]+).*' $"$2,$1" | from csv -n | each {|r| $r.column1 + $r.column2} which of course relies on the same regex library (hattip).
- ogogmad 4y agoIt can't be comfortably used as part of a larger Awk script.
- burntsushi 4y agoYes, as I acknowledged generally in the end of my comment.