Search code examples
regexlinuxmacosack

Equivalent of a "mdfind" query in Ack: AND operator + -interpret switch


I am trying to modify Brett Terpstra's handy QuickQuestion script for my Linux machine. What would be Ack's equivalent to the following mdfind commands:

mdfind -onlyin "$NOTESDIR" "filename:.$NOTESEXT AND filename:\"$NOTESPRE\" AND ${INPUT%\?}"
...
echo "`mdfind -onlyin \"$NOTESDIR\" -interpret \"filename:.$NOTESEXT AND filename:$NOTESPRE AND ${INPUT%\?}\"`"

I can't seem to get the AND part right. I understand that one has to use parentheses since Ack is Perl. But for example (?="$NOTESPRE")(?="$INPUT") gave errors.

Furthermore, I am not sure what to do with the -interpret switch on the second line.

Puzzled with such a simple query... Thanks for any help!


Solution

  • Double lookaheads need a range of things to consider, so you need to add wildcards to their patterns, for example:

    >>echo "foo bar \$NOTESPRE \$INPUT baz" | ack "^(?=.*$NOTESPRE)(?=.*$INPUT).*$"
    

    returns:

    foo bar $NOTESPRE $INPUT baz
    

    If you need to search a particular directory for files containing this line, just specify the directory on the command line:

    ack "^(?=.*$NOTESPRE)(?=.*$INPUT).*$" path\to\directoryToSearch
    

    -interpret has no direct parallel in regex.