Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ag for multiple patterns #551

Closed
c02y opened this issue Dec 2, 2014 · 12 comments
Closed

ag for multiple patterns #551

c02y opened this issue Dec 2, 2014 · 12 comments

Comments

@c02y
Copy link

c02y commented Dec 2, 2014

For example, I got files which one or more of them contains:

...
one
two three
four
five
...

I want to know which files contains one AND four(which may be in the different lines) at the same time,
I can use grep -e one -e four /dir/files* to get this, can I do that using ag?

@ZyX-I
Copy link

ZyX-I commented Dec 9, 2014

You can use ag 'one|four'.

@ggreer
Copy link
Owner

ggreer commented Dec 12, 2014

Thanks for the solution, ZyX-I.

@ggreer ggreer closed this as completed Dec 12, 2014
@c02y
Copy link
Author

c02y commented Dec 20, 2014

@ZyX-I
Although grep -e one -e four /dir/files* means OR too.
The '|' in ag 'one|four' means OR, I want AND.

@ZyX-I
Copy link

ZyX-I commented Dec 20, 2014

@c02y You asked the equivalent for ­-e -e, you got it. If you needed some other feature you should not have mentioned ­-e -e. To get your AND with grep you need to do two searches. With ag you may use one regex: '(?=(?:.|\n)*?one)(?:.|\n)*?four' or one(?:.|\n)*?four|four(?:.|\n)*?one.

Also you do not want AND. AND in regexes can be expressed via one\&four (Vim, not ag) with the PCRE equivalent (?=one)four which means “both one and four matched in one position” which obviously never matches any string. Regexes do not work file-wise and I suggest to use --files-with-matches with my regex or with grep calls because otherwise output is rather lengthy, contains lines that have neither one nor four and may not contain all lines with one (only relevant for the first regex, second one will print everything starting from the first line with one or four found till the nearest line with four or one respectively, repeated).

@danthegoodman
Copy link

For others who would like something similar, I accomplished this by chaining multiple ag calls together using -l and xargs:

ag -l one | xargs ag four

This has the downside of only highlighting the term in the last query. If you want to hightlight both, this will work:

ag -l one | xargs ag -l four | xargs ag 'one|four'

@c02y
Copy link
Author

c02y commented Jun 26, 2017

@danthegoodman
It seems

ag -l one | xargs ag -l four | xargs ag 'one|four'

only prints the matching lines number but not the file name if one file matches the pattern.

@HaleTom
Copy link
Contributor

HaleTom commented Oct 6, 2017

I needed to add --no-color as ag writes colours through pipes otherwise, and this was preventing the subsequent ag matching properly.

ag -l --no-color one | xargs ag -l --no-color four | xargs ag --filename 'one|four'

Bug #1164 prevents printing the filename if only one filename is passed to the final ag.

@CMCDragonkai
Copy link

What about when you want files that mention 2 words (but those 2 words can be anywhere in the file).

@pabloab
Copy link

pabloab commented May 18, 2018

@HaleTom on ag version 0.31.0 is --nocolor (without the dash).

Now would be nice pattern1 AND NOT(pattern2). I suspect -v...

@adrianTNT
Copy link

adrianTNT commented Dec 18, 2019

For others who would like something similar, I accomplished this by chaining multiple ag calls together using -l and xargs:

ag -l one | xargs ag four

!! careful about this, I noticed if used like this ag -li word1 my_directory/ | xargs ag -li word2, works nicely, but if first part finds nothing, second part it will search in current directory, not in my_directory, since first part will output blank, it will simply run ag li word2 with no file to search into = current directory; and you cannot include the path in second part, it would re-do the search in that directory.

Solution for that seems to be --no-run-if-empty:

ag -li word1 my_directory/ | xargs --no-run-if-empty ag -li word2 

@b0o
Copy link

b0o commented Jun 8, 2020

I wrote a little script which integrates some of the suggestions here and provides more features: aag.

@peterbabic
Copy link

Why -e is not implemented, BTW? Cluttering?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants