Ack trick of the day

0 views
Skip to first unread message

Andy Lester

unread,
Aug 27, 2008, 9:26:35 AM8/27/08
to ack-...@googlegroups.com
I've got a bunch of programs that aren't properly including include
files. In this case I want to find all PHP code that calls DBLogin()
but doesn't include dblogin.php.

$ ack -vl dblogin.php $(ack DBLogin -l)

xoxo,
Andy

--
Andy Lester => an...@petdance.com => www.petdance.com => AIM:petdance


Will Coleda

unread,
Aug 27, 2008, 9:34:56 AM8/27/08
to ack-...@googlegroups.com

I was *just* wondering how to do something like that with ack on
windows. Ended up having to write a perl program.

Be nice if there were a way to specify the files to search through in
a separate file, something like:

ack -l DBLogin > searchme
ack -vl "dblogin\.php"
--long-option-to-specify-filename-which-contains-filenames-to-search
searchme

Regards.

--
Will "Coke" Coleda

Andy Lester

unread,
Aug 27, 2008, 9:42:08 AM8/27/08
to ack-...@googlegroups.com

On Aug 27, 2008, at 8:34 AM, Will Coleda wrote:

> Be nice if there were a way to specify the files to search through in
> a separate file, something like:


Agreed. If there's not a ticket for that, make one.

Ori Avtalion

unread,
Aug 27, 2008, 2:59:14 PM8/27/08
to ack-...@googlegroups.com
On Wed, Aug 27, 2008 at 4:26 PM, Andy Lester <an...@petdance.com> wrote:
>
> I've got a bunch of programs that aren't properly including include
> files. In this case I want to find all PHP code that calls DBLogin()
> but doesn't include dblogin.php.
>
> $ ack -vl dblogin.php $(ack DBLogin -l)

According to the docs, -L can be used instead of -vl to make this
command even shorter :)

I must admit this behavior is a bit unexpected. My first guess would
be to assume that if -v prints lines that do not match, -l will print
files which had lines that did not match. It would even print the
files who did have the searched pattern, as long as non-matching lines
were present.

From my simple of looking for 'b' in a file containing 'a\nb\nc\d', it
would seem that grep -vl behaves as I described.
If ack handles -vl/-L in a special way, perhaps it should be clarified.

... Or maybe it's just me being silly again :)

-Ori

Andy Lester

unread,
Aug 27, 2008, 3:08:27 PM8/27/08
to ack-...@googlegroups.com
>
> According to the docs, -L can be used instead of -vl to make this
> command even shorter :)

I forgot about that, and it makes sense, too. The author must have
been pretty smart to put that in there! :-)

-L is the negation of -l, is how to think of it.

xoa

Christopher J. Madsen

unread,
Aug 27, 2008, 11:16:32 PM8/27/08
to ack-...@googlegroups.com
Andy Lester wrote:
> -L is the negation of -l, is how to think of it.

I think you may have overlooked Ori's point, which is this: To ack, -vl
and -L are equivalent (I think; I haven't looked through the internals),
but to grep, they mean different things. If we really mean for ack to
work differently than grep, we should state so more clearly. I vote for
changing ack to work like grep, because I can't figure out how to get
grep's -vl behavior out of ack as it currently stands.

Say you have 4 files (each letter is on a separate line):
a.txt: a b c
b.txt: b b b
c.txt: a c
empty.txt: (empty.txt is a 0 byte file)

$ grep -l b *.txt
a.txt
b.txt

$ ack -l b *.txt
a.txt
b.txt

So these work the same. "Print files with a line matching b."

$ grep -L b *.txt
c.txt
empty.txt

$ ack -L b *.txt
c.txt

Minor difference. Grep thinks "Print files without a line matching b,"
while ack adds "but that have at least one line".

$ grep -vl b *.txt
a.txt
c.txt

$ ack -vl b *.txt
c.txt

Here we see the first major difference. To grep, it means "Print files
with at least one line that does not match b." To ack, it means the
same as the previous command (as best I can tell).

$ grep -vL b *.txt
b.txt
empty.txt

$ ack -vL b *.txt
c.txt

Another difference. To grep, this means "Print files whose every line
matches b." (-v means "select lines that don't match b", and -L means
"print files from which no lines were selected") To ack, it seems to be
the same as the previous two.

--
Chris Madsen pe...@cjmweb.net
-------------------- http://www.cjmweb.net --------------------

Reply all
Reply to author
Forward
0 new messages