How to improve text search efficiency in Linux?

Source: Internet
Author: User
Tags perl script
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

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.

 
 
  1. ___FCKpd___0nbsp;wget http://beyondgrep.com/ack-2.12-single-file 
  2. ___FCKpd___0nbsp;sudo mv ack-2.12-single-file /usr/local/bin/ack 
  3. ___FCKpd___0nbsp;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:

 
 
  1. ___FCKpd___1nbsp;ack -w eat 

2. search for strings with special characters '$ path =.'. all metacharacters (such as '$', '.') must be matched literally:

 
 
  1. ___FCKpd___2nbsp;ack -Q '$path=.' /etc 

3. in addition to the dowloads Directory, search for the word "about" in all directories:

 
 
  1. ___FCKpd___3nbsp;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:

 
 
  1. ___FCKpd___4nbsp;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:

 
 
  1. ___FCKpd___5nbsp;ack --make -l CFLAG 

6. highlight the matching string when displaying the entire log file:

 
 
  1. ___FCKpd___6nbsp;tail -f /var/log/syslog | ack --passthru 192.168.1.10 

7. in exchange for the file filtering types supported by ack, run:

 
 
  1. ___FCKpd___7nbsp;ack --help-type 
Related Article

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.