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

--files-without-matches does not work properly #238

Open
jdv85 opened this issue Jul 26, 2013 · 15 comments
Open

--files-without-matches does not work properly #238

jdv85 opened this issue Jul 26, 2013 · 15 comments

Comments

@jdv85
Copy link

jdv85 commented Jul 26, 2013

-l and -L should be mutually exclusive, but they are not.

A simple example illustrating the issue:

zsh 12550 % echo test123 > file
zsh 12551 % ag -L test file 
file
zsh 12552 % ag -l test file
file
zsh 12548 % ag --version
ag version 0.15
@mcphail
Copy link
Contributor

mcphail commented Jul 27, 2013

This is a duplicate of issue #206. Please test and report on my patch in pull request #211 to see if it helps. Thanks.

@joshma
Copy link

joshma commented May 28, 2015

I know it's 2 years later, but this still seems like an issue? The original test case now passes, but here's a minimal repro, it seems to require a trailing newline:

$ echo 'foo\n' > file
$ ag -L foo file
file
$ ag -l foo file
file

@jez
Copy link

jez commented Jun 14, 2015

@joshma I see this as an issue too. I can reproduce the above and I run into the issue on a separate instance.

decaff added a commit to decaff/the_silver_searcher that referenced this issue Jun 19, 2015
1) Bug fix for issue ggreer#238 where a file (call it test.txt) of contents
  "foo\n\n" will be invert matched by this command:

      ag -L foo test.txt

2) If user specifies both -L and -v on command line, that's a conflict.
   Resolve by suppressing -v .

3) Added test cases for 1 and 2.
@decaff
Copy link

decaff commented Jun 19, 2015

Interesting problem. I believe this commit makes -L work as expected. Give it a shot...let me know if it doesn't work as expected.

@joshma
Copy link

joshma commented Jun 19, 2015

@decaff works for me (although obviously no idea if it breaks anything else) :)

@brianloveswords
Copy link

I just ran into this today! Is it possible to cut a new release with this fix sometime soon?

@KingDuckZ
Copy link

$ ag --version
ag version 0.31.0

Not sure if those commits are in my version yet, but I found a problem that breaks my bash script around ag. This used to work:

DeleteFile=$(ag --files-with-matches "^$MatchRegex\$" "$CopiedList" | wc -l)
if [ $DeleteFile -eq 0 ]; then
    DeleteFilePath=$(echo "$OutDir/$z" | sed --regexp-extended 's%/{2,}%/%g')
    rm -rf "$DeleteFilePath"
fi

Now my script deletes all files because when invoked with an explicit file name, ag doesn't print the match anymore.

[michele@weblite tmp]$ cd /tmp/
[michele@weblite tmp]$ ls
systemd-private-39809ec5dd1c4aa9bac4ba7ef7fae5a7-systemd-timesyncd.service-WoTS23  tmp.HdtQ3IGW3u  tmp.WyOjJBDNa4
[michele@weblite tmp]$ ag -l 'risorse/misaligned.css' .
ERR: Error opening directory ./systemd-private-39809ec5dd1c4aa9bac4ba7ef7fae5a7-systemd-timesyncd.service-WoTS23: Permission denied
tmp.WyOjJBDNa4
tmp.HdtQ3IGW3u
[michele@weblite tmp]$ ag -l 'risorse/misaligned.css' tmp.*
tmp.HdtQ3IGW3u
tmp.WyOjJBDNa4
[michele@weblite tmp]$ ag -l 'risorse/misaligned.css' tmp.HdtQ3IGW3u 
[michele@weblite tmp]$ ag -l 'risorse/misaligned.css' tmp.WyOjJBDNa4 
[michele@weblite tmp]$ ag -l 'risorse/misaligned.css' tmp.WyOjJBDNa4 | wc -l
0

That used to work (but unfortunately I don't know until what version). ag should still return the file name to confirm there was a match.

Edit Should I open a new issue? I posted here because somehow it looks related to this issue to me, but if I'm wrong I'll file a new bug.

Edit 2 I see the return code can be used to test if the match was successful, the problem is that ag will exit with code 1 either if there was no match or if the file couldn't be accessed, so it's confusing.

@ilmari
Copy link
Contributor

ilmari commented Oct 14, 2015

This is still an issue in git master (comm -12 shows lines that are common between the two inputs):

$ git describe --tags 
0.31.0-21-gb0eb6d7
$ comm -12 <(./ag -l Silver|sort) <(./ag -L Silver|sort)
doc/ag.1.md
NOTICE
README.md

@adius
Copy link

adius commented Jan 26, 2016

What's the status on this one?

@ilmari
Copy link
Contributor

ilmari commented May 19, 2016

It's still an issue with current master (630125a). The issue seems to be that -L ignores matches on the first line of the file.

$ ./ag Silver
README.md
1:# The Silver Searcher

doc/ag.1.md
1:ag(1) -- The Silver Searcher. Like ack, but faster.

doc/ag.1
7:\fBag\fR \- The Silver Searcher\. Like ack, but faster\.

NOTICE
1:The Silver Searcher

the_silver_searcher.spec.in
19:The Silver Searcher

@Apakollaps
Copy link

First off, ag is awesome so thanks everyone who's involved for your work. It makes me surprised that this issue is still open though, as it makes one doubt the results from ag. Is there an ETA for a fix?

@serhalp
Copy link

serhalp commented Jan 9, 2018

Any updates on this? -L completely ignores matches on the first line of the file. Seems like if this isn't going to be fixed soon, the flag should be removed entirely, as it effectively does not work as advertised at all.

@tukanos
Copy link

tukanos commented Sep 19, 2018

Still valid today. What was reported by @serhalp is still true.

@davidstosik
Copy link

davidstosik commented Jan 28, 2019

Still an issue... I spent 30 minutes trying to understand why a file was showing in both ag -l and ag -L...

$ ag --version
ag version 2.2.0

Features:
  +jit +lzma +zlib

$ echo -e "foo\nbar" > file.txt

$ cat file.txt
foo
bar

$ ag --files-with-matches bar file.txt
file.txt

$ ag --files-without-matches bar file.txt
file.txt

$ echo "" >> file.txt

$ cat file.txt
foo
bar


$ ag --files-with-matches bar file.txt
file.txt

$ ag --files-without-matches bar file.txt
# No result

Looks like ag -L ignores a file's last line.
grep gets it right:

$ grep --version
grep (GNU grep) 3.1
Packaged by Homebrew
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Mike Haertel and others, see <http://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>.

$ echo -e "foo\nbar" > file.txt

$ cat file.txt
foo
bar

$ grep -l bar file.txt
file.txt

$ grep -L bar file.txt
# No result

@jwhitley
Copy link

I believe this issue should be closed, as a dupe of many other issues, because it should be fixed once and for all by #1245, which has been merged for some months now. /cc @ggreer

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