Turn: How to improve the search efficiency of text on Linux

Source: Internet
Author: User
Tags ack perl script

Originally from: http://www.geekfan.net/6881/

For system administrators or programmers, the grep type tool is probably the most popular when it is necessary to search for a specific text or pattern in a directory of complex configurations or in a large source tree.

If grep is one of your favorite tools, you might prefer an ACK. ACK is a Perl-like command-line tool similar to grep, but searches faster and is more powerful than grep. Especially when you're a programmer, I highly recommend that you use ACK instead of grep.

The use of ACK is well suited for searching with code, so programmers can make complex queries in the source tree, with fewer keystrokes required.

Characteristics of ACK

Some of the very powerful features of ACK:

    • Default search Current working directory
    • Default Recursive search sub-directory
    • Ignore metadata directories, such as. Svn,.git,csv directories
    • Ignore binary files (such as pdf,image,coredumps) and backup files (e.g. FOO~,*.SWP)
    • Print line numbers in search results to help find the target code
    • Ability to search for specific file types (such as Perl,c++,makefile), which can have multiple file suffixes
    • Highlight search Results
    • High-level regular expressions that support Perl are more expressive than the GNU regular expressions used by grep.

The ACK is generally faster than grep compared to the search speed. The speed of the ACK is as long as it behaves in its built-in file type filter. During the search process, ACK maintains a list of recognized file types while skipping unknown or unnecessary file types. It also avoids checking for redundant metadata directories.

Installing the ACK on Linux

Although the ACK is a standard package in most Linux distributions, it can be easily obtained (for example, in a Debian-based system, it is a ACK-GREP packet, and an ACK packet in a redhat-based system), but the ACK version bundled with the release is still 1.x. And ack2.0 has been released, and has more features.

So I'm going to download it on the official website and then install the ACK.

Conveniently, ACK is available as a separate Perl script in the official website, which incorporates all the modules that need to be relied upon. Therefore, you do not need to install additional Perl modules to run this script.

To install the ACK on your Linux system, go to the official website to download the latest version of the ACK. At the time of writing this article, the latest version is 2.12

1234 $ 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

It is important to note that in a Debian-based system, there is a separate package called an ACK (Han code Converter). So if you happen to have that package, then you have to rename the ACK to avoid naming conflicts.

Use cases for ACK

1. Recursively searches the current directory for the word "eat", and does not match a string similar to "feature" or "eating":

12 $ ack -w eat

2. Search for strings with special characters ' $path =. ', all metacharacters (e.g. ' $ ', '. ') Need to be literally matched:

12 $ ack -Q ' $path =. ' /etc

3. In addition to the Dowloads directory, search for "about" words in all directories:

12 $ ack about --Ignore-dir=downloads

4. Search only for PHP files that contain ' protected ' words, then combine the search results by file name to print the search results for each file:

12 $ ack-- php --group protected

5. Get the file name of the makefile that contains the ' cflag ' keyword. The file name *.mk,makefile,makefile,gnumakefile is within the scope of the consideration:

12 $ ack- -make -l cflag

6. The string to highlight when displaying the entire log file:

12 $ tail -F /var/Log/syslog | ack --passthru 192.168.1.10

7. To exchange the type of file filtering supported by ACK, run:

12 $ ack --help-type



Original link: Xmodulo translation: Geek Van-Minejo

Link: http://www.geekfan.net/6881/

Turn: How to improve the search efficiency of text on Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.