Ripgrep: Is it possible to find files that match some pattern and don't match another?

Created on 8 Jun 2018  路  3Comments  路  Source: BurntSushi/ripgrep

This command finds all package.json files that declare a dependency on chalk:

rg -F '"chalk":' -g package.json --files-with-matches

This command finds all private packages:

rg -F '"private": true' -g package.json --files-without-match

If there a way to write a rg command that combines both? I.e., "list files that contain "chalk": but don't contain "private": true". Thanks.

duplicate

Most helpful comment

Not with a single call to rg, but you can do it by combining it with other shell commands. This will give you the intersection between the two result sets:

{
  rg -F '"chalk":' -g package.json --files-with-matches &
  rg -F '"private": true' -g package.json --files-without-match
} | awk 'seen[$0]++ == 1' # or `sort | uniq -d` if you don't mind waiting for output

All 3 comments

Not with a single call to rg, but you can do it by combining it with other shell commands. This will give you the intersection between the two result sets:

{
  rg -F '"chalk":' -g package.json --files-with-matches &
  rg -F '"private": true' -g package.json --files-without-match
} | awk 'seen[$0]++ == 1' # or `sort | uniq -d` if you don't mind waiting for output

Thanks, I'll probably go that route. Wanted to ask first as doing it in one command would make it 2x faster.

@okdana I love that awk trick.

@borekb If this were to be fixed, it would be fixed by #875, if I decide to do it. Still unsure. So I'm going to close this as a duplicate.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Offpics picture Offpics  路  3Comments

bastienbc picture bastienbc  路  3Comments

andschwa picture andschwa  路  3Comments

mllg picture mllg  路  3Comments

davidtwco picture davidtwco  路  3Comments