CentOS6 The Silver Searcher(ag.exe)ソースビルド方法

Windows版はこっちを参考にどうぞ
ほとんど同じ内容です(手抜き

Windows Cygwin環境のThe Silver Searcher(ag.exe)ビルド方法 - GeekなNooblog
http://d.hatena.ne.jp/sona-zip/20130531/p1

grepに変わるack、、、に変わるagとして最近話題のagについての紹介です。

The Silver Searcherの特徴

公式に書いてあるThe Silver Searcherの特徴

ackの3〜5倍高速
.gitignore、.hgignoreに記載されているものを検索対象から除外
検索対象から除外したいファイルは.agignoreに記載
agというコマンド名で、ackと比べてコマンドが短い(33%減!)

ackを捨てて、より高速なag(The Silver Searcher)に切り替えた - Glide Note - グライドノート

ビルドがうまくいかない方は./build.sh実行後に以下のようなエラーが出ているかと思います。

checking pkg-config is at least version 0.9.0... yes
checking for PCRE... yes
checking for LZMA... configure: error: Package requirements (liblzma) were not met:

No package 'liblzma' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables LZMA_CFLAGS
and LZMA_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

lzma関連のパッケージを正しくインストールすると解決しますが、以降の解説ではビルド手順を最初から説明します。

必要なパッケージのインストール

LZMAにxz-develが必要だとわからず苦労しましたが、Twitterで教えていただいたことにより無事インストールができました。

yum -y install gcc make autoconf automake git zlib zlib-devel pkgconfig 
pcre pcre-devel lzma lzma-devel lzma-libs xz xz-devel xz-libs

the Silver Searcherのビルド

mkdir /usr/local/src
cd !$
git clone https://github.com/ggreer/the_silver_searcher.git
cd the_silver_searcher
./build.sh

the Silver Searcherの配置

$ make install
$ which ag
/usr/local/bin/ag


以上で完了です。

使い方

よく使うような-wや-iなどはgrepと同じように使うことができますが、agコマンドではデフォルトでカレントディレクトリ以下の再起探索(./ -r)とカラー(--color)が有効になっています。
またagは同じファイル内の検索結果はまとめて表示されます。(--group)
コマンドで表すと以下の2つが同じような出力結果となります。

grep SEARCH_WORD ./ -r --color
ag SEARCH_WORD --nogroup


普段はこれだけのコマンドで目的の検索ができてしまいます。

ag SEARCH_WORD


詳細についてはag --helpで確認してみてください。

$ ag
Usage: ag [OPTIONS] PATTERN [PATH]

Recursively search for PATTERN in PATH.
Like grep or ack, but faster.

Example: ag -i foo /bar/

Search options:

--ackmate               Print results in AckMate-parseable format
-a --all-types          Search all files (doesn't include hidden files
                        or patterns from ignore files)
-A --after [LINES]      Print lines before match (Default: 2)
-B --before [LINES]     Print lines after match (Default: 2)
--[no]break             Print newlines between matches in different files
                        (Enabled by default)
--[no]color             Print color codes in results (Enabled by default)
--color-line-number     Color codes for line numbers (Default: 1;33)
--color-match           Color codes for result match numbers (Default: 30;43)
--color-path            Color codes for path names (Default: 1;32)
--column                Print column numbers in results
--line-numbers          Print line numbers even for streams
-C --context [LINES]    Print lines before and after matches (Default: 2)
-D --debug              Ridiculous debugging (probably not useful)
--depth NUM             Search up to NUM directories deep (Default: 25)
-f --follow             Follow symlinks
--[no]group             Same as --[no]break --[no]heading
-g PATTERN              Print filenames matching PATTERN
-G, --file-search-regex PATTERN Limit search to filenames matching PATTERN
--[no]heading
--hidden                Search hidden files (obeys .*ignore files)
-i, --ignore-case       Match case insensitively
--ignore PATTERN        Ignore files/directories matching PATTERN
                        (literal file/directory names also allowed)
--ignore-dir NAME       Alias for --ignore for compatibility with ack.
-l --files-with-matches Only print filenames that contain matches
                        (don't print the matching lines)
-L --files-without-matches
                        Only print filenames that don't contain matches
-m --max-count NUM      Skip the rest of a file after NUM matches (Default: 10,000)
--no-numbers            Don't show line numbers
-p --path-to-agignore STRING
                        Use .agignore file at STRING
--print-long-lines      Print matches on very long lines (Default: >2k characters)
-Q --literal            Don't parse PATTERN as a regular expression
-s --case-sensitive     Match case sensitively (Enabled by default)
-S --smart-case         Match case insensitively unless PATTERN contains
                        uppercase characters
--search-binary         Search binary files for matches
--stats                 Print stats (files scanned, time taken, etc.)
-t --all-text           Search all text files (doesn't include hidden files)
-u --unrestricted       Search all files (ignore .agignore, .gitignore, etc.;
                        searches binary and hidden files as well)
-U --skip-vcs-ignores   Ignore VCS ignore files
                        (.gitigore, .hgignore, .svnignore; still obey .agignore)
-v --invert-match
-w --word-regexp        Only match whole words
-z --search-zip         Search contents of compressed (e.g., gzip) files

参考

ggreer/the_silver_searcher · GitHub
https://github.com/ggreer/the_silver_searcher