For system administrators or programmers, grep is probably the most popular tool when they need to search for specific text or patterns in a directory with complex configurations or in a large source code tree.
If grep is one of your favorite tools, you may prefer ack. Ack is a Perl-based command line tool similar to grep, but the search speed is faster and the capability is stronger than grep. Especially when you are a programmer, I strongly recommend you replace grep with ack.
Ack is very useful for code search. Therefore, programmers can perform complex queries in the source code tree, instead of requiring fewer keys.
Features of ack
Some very powerful features of ack:
The current working directory is searched by default.
Default recursive search subdirectory
Ignore metadata directories, such as. svn,. git, and CSV.
Ignore binary files (such as pdf, image, coredumps) and backup files (such as foo ~, *. Swp)
Print the row number in the search result to help you find the target code.
You can search for specific file types (such as Perl, C ++, and Makefile). This file type can have multiple file suffixes.
Highlight search results
Supports Perl high-level regular expressions, which are more expressive than the GNU regular expressions used by grep.
Compared with search speed, ack is generally faster than grep. As long as the ack speed is manifested in its built-in file type filter. During the search process, ack maintains a list of accepted file types and skips unknown or unnecessary file types. It also avoids checking excessive metadata directories.
Install ack on Linux
Although ack is a standard package in most Linux distributions, it can be easily obtained (for example, In debian-based systems, it is an ack-grep package, in the Redhat-based system, it is an ack package), but the ack version bound to the release version is still 1.x, and ack2.0 has been released and has more features.
So I am going to download it from the official website and install ack.
It is convenient that ack is obtained as a separate Perl script on the official website Coco, which integrates all the modules that need to be depended on. Therefore, you do not need to install the Perl module to run this script.
To install ack on your Linux system, go to the official website to download the latest ack version. When writing this document, the latest version is 2.12.
$ Wget http://beyondgrep.com/ack-2.12-single-file $ sudo mv ack-2.12-single-file/usr/local/bin/ack $ sudo chmod 0755/usr/local/bin/ack
Note that in Debian-based systems, an independent package is also called ack ). So if you happen to use that package, you must rename ack to avoid naming conflicts.
Use Cases of ack
1. Recursively search for the word "eat" in the current directory and do not match strings similar to "feature" or "eating:
$ Ack-w eat
2. Search for strings with special characters '$ path =.'. All metacharacters (such as '$', '.') must be matched literally:
$ Ack-Q '$ path =.'/etc
3. In addition to the dowloads directory, search for the word "about" in all directories:
$ Ack about -- ignore-dir = downloads
4. Search for only the PHP file containing the word 'protected', and then combine the search results by file name to print the corresponding search results for each file:
$ Ack -- php -- group protected
5. Get the name of The Makefile containing the 'cflag' keyword. The file names *. mk, makefile, Makefile, and GNUmakefile are all within the scope of consideration:
$ Ack -- make-l CFLAG
6. Highlight the matching string when displaying the entire log file:
$ Tail-f/var/log/syslog | ack -- passthru 192.168.1.10
7. In exchange for the file filtering types supported by ack, run:
$ Ack -- help-type
Original article: Xmodulo Translation: geek fan-minejo
Http://www.geekfan.net/6881/.