Ripgrep: Add an option similar to -o, --only-matching

29

With grep you can print only the matched parts of the files. The option is described like this in the grep manpage:

-o, --only-matching
       Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.

A more powerful option, would something similar to --replace, but that doesn't print the non matched part of the text.

dzamlo picture dzamlo  ·  23 Sep 2016

Most helpful comment

36

To counterbalance the above comment, I wanted to say that I use and enjoy this excellent tool you've created every day, and I appreciate whatever improvements are made whenever they are made. Thanks for using some of your limited free time to make my life and others' easier.

gavinfoley picture gavinfoley  ·  15 Dec 2016

All comments

0

Note that surrounding the search regex with ._( )._ and then using --replace $1 will do this; pretty verbose though, especially when using it in a pipeline.

cwillu picture cwillu  ·  24 Sep 2016
2

--replace already exists (and I agree it's kind of verbose for this case).

-o/--only-matching is an option on grep, so I'm OK adding this.

BurntSushi picture BurntSushi  ·  24 Sep 2016
0

Input

host -t A wikipedia.org | openssl dgst -sha256 | rg --no-line-number [A-Fa-f0-9]{64}

Output

(stdin)= 0d00409623da8ae039de83200af7f6fa9211bb13e1b3e10ea45142cd08b3e50b

Hash is coloured red, but I need hash (that red part) only.
Would the considered option --only-matching be of help?

sergeevabc picture sergeevabc  ·  15 Dec 2016
1

@sergeevabc Yes, it would solve your problem. But there is a work-around for now:

$ host -t A wikipedia.org | openssl dgst -sha256 | rg -N '^.*([A-Fa-f0-9]{64}).*$' --replace '$1'
BurntSushi picture BurntSushi  ·  15 Dec 2016
0

@BurntSushi, thanks, but when --only-matching will be implemented?

sergeevabc picture sergeevabc  ·  15 Dec 2016
4

@sergeevabc I don't know. It happens when it happens. I do this in my free time and I don't typically schedule my free time in advance. Sorry.

BurntSushi picture BurntSushi  ·  15 Dec 2016
36

To counterbalance the above comment, I wanted to say that I use and enjoy this excellent tool you've created every day, and I appreciate whatever improvements are made whenever they are made. Thanks for using some of your limited free time to make my life and others' easier.

gavinfoley picture gavinfoley  ·  15 Dec 2016
29

@sergeevabc This issue tracker isn't place to give me advice on how to schedule my free time or to shame anyone into doing work on their free time. Future comments in that vain won't be tolerated.

BurntSushi picture BurntSushi  ·  15 Dec 2016
8

Wonderful, thank you for the great work @kpp + @BurntSushi 🎉

22a picture 22a  ·  9 Apr 2017