Ack

What is ack?

ack is a wonderful replacement for grep.

Usage / Example:

ack [OPTIONS] PATTERN [FILES OR DIRECTORIES]

ack --type=java --type=jsp --type=nojs --type=nocss -h '<script ' &> /c/Khai/output.txt

If we do not specify files or directories, ack will recursively search the current directory. If we specifies files or directories, ack will recursively search only those files and directories. ack may also search STDIN, but only if no file or directory arguments are specified, or if one of them is -.

By default ack will prefix the matching line with the name of the file. The -h option tells ack not to prefix the matching line with the name of the file. The -t option tells act to only examine Java files.

How can we tell ack to only display the name of the files that matches?

ack -l pattern

Use the -l switch (the lowercase letter L). By default, ack display the name of the files, and the lines that matches. To tell ack to only display the name of the files, use the -l switch or --files-with-matches.

How does ack work? What is its default behavior?

Search for PATTERN in each source file in the tree from the current directory on down. If any files or directories are specified, then only those files and directories are checked. ack may also search STDIN, but only if no file or directory arguments are specified, or if one of them is "-".

What is the purpose of the .ackrc file? How can we instruct ack to ignore the .ackrc file?

Default switches may be specified in an .ackrc file. If you want no dependency on the environment, turn it off with —noenv.

How can we install ack if we want to use it inside Git Bash?

mkdir ~/bin
curl https://beyondgrep.com/ack-2.22-single-file > ~/bin/ack && chmod 0755 ~/bin/ack

What are available options?

  1. -i ( --ignore-case )
  2. -v ( --invert-match ): select non-matching lines
  3. -w ( --word-regexp ): Force pattern to match only whole words
  4. -Q ( --literal ): Quote all metacharacters: pattern is literal
  5. --lines=NUM : Only print NUM lines of each file

See https://kapeli.com/cheat_sheets/Ack.docset/Contents/Resources/Documents/index

How can we tell ack to print only the name of the files that match our search pattern?

Use the -l switch:

ack -l

See https://serverfault.com/questions/57321/how-do-i-show-filenames-only-after-a-keyword-grep-search

My .ackrc file:

--type-add=php=.tpl
--ignore-dir=external
#make sure ack knows how to search common filetypes used in rails projects
--type-add=css=scss
--type-add=ruby=.haml,.rselm,.feature,.ru,.lock
--type-set=coffeescript=.coffee
--type-set=coffee=.coffee

#make sure ack knows how to search common filetypes used in node.js projects
--ignore-dir=node_modules
--type-set=coffee=.coffee
--type-set=jade=.jade
--type-set=feature=.feature
--type-set=json=.json

Can we have per directory or per project .ackrc file?

Yes. We can have per directory or per project .ackrc by putting:

export ACKRC=".ackrc"

into your .bashrc or .bash_profile file.

How can we tell ack to ignore the .ackrc file?

If we want to ignore the .ackrc file with the —noenv option:

ack --noenv pattern

http://beyondgrep.com/documentation/

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License