速度和激情*nix
命令行总是很方便:
# grep --line-number \\
--exclude-dir=/path/to/some/directory \\
--include=*.php \\
--recursive \\
"add_filter\\|do_action\\|apply_filters" \\
/path/to/wp-content/plugins/some-plugin \\
| less
更多选项通过
#man grep
.
然后我们甚至可以创建一个简单的bash脚本wp-search.sh
:
#!/bash/bin
grep --line-number \\
--exclude-dir=/path/to/some/directory \\
--include=*.$1 \\
--recursive $2 $3
并运行它。
# bash wp-search.sh php "add_filter\\|do_action\\|apply_filters" /path/to/some-plugin
漂亮的输出我们可以使用
--color
属性为的输出着色
grep
, 但请注意,它不会与
less
.
另一种选择是为搜索结果生成HTML表。
这是一个awk
我构建的示例将搜索结果作为HTML表输出到results.html
文件:
| sed \'s/:/: /2\' \\
| awk \' \\
BEGIN { \\
print "<table><tr><th>Results</th><th>Location</th></tr>" \\
} \\
{ \\
$1=$1; location=$1; $1=""; print "<tr><td>" $0 "</td><td>" location "</td><tr>" \\
} \\
END { \\
print "</table>" \\
}\' \\
> results.html
我使用过的地方
this trick 要删除所有前导空格,请执行以下操作:
this one 打印除第一个字段外的所有字段。
我使用sed
此处仅在第二个冒号后添加额外的空格(:
), 以防万一那里没有空间。
我们可以将此脚本添加到wp-search.sh
脚本:
#!/bash/bin
grep --with-filename \\
--line-number \\
--exclude-dir=/path/to/some/directory \\
--include=*.$1 \\
--recursive $2 $3 \\
| sed \'s/:/: /2\' \\
| awk \' BEGIN { \\
print "<table><tr><th>Results</th><th>Location</th></tr>" \\
} \\
{ \\
$1=$1; location=$1; $1=""; print "<tr><td>" $0 "</td><td>" location "</td><tr>" \\
} \\
END { \\
print "</table>" \\
}\' \\
> /path/to/results.html
您必须调整
/path/to/some/directory
和
/path/to/results.html
满足您的需求。
示例-如果我们在wordpress-importer
插件具有:
bash wp-search.sh php "add_filter\\|do_action" /path/to/wp-content/plugins/wordpress-importer/
然后
results.html
文件将显示为:

示例-搜索岩芯
我对岩芯进行了时间测试:
time bash wp-search.sh php "add_filter\\|do_action" /path/to/wordpress/core/
real 0m0.083s
user 0m0.067s
sys 0m0.017s
而且速度很快!
注意:为了获得额外的上下文,我们可以使用-C NUMBER
格雷普的。
我们可以用各种方式修改HTML输出,但希望您可以根据需要进一步调整。