我们希望通过find
命令将grep
应用到选定文件时显示每个文件的修改date和时间。 最终的结果应该是这样的:
2016-10-17 Mon 20:38:57 ./rest/47results.php: 5 :σχόλια, ιδέες facebook
从47test.php文件运行以下内容:
system('export TZ=":Europe/Athens"; find . -name "*.*" \ -not \( -path ./admin -prune \) \ -not \( -path ./people/languages -prune \) \ -not \( -path ./include -prune \) \ -type f -mmin -10 \ -printf "%TY-%Tm-%Td %Ta %TH:%TM:%TS %p\n" \ -exec grep -HTni "σχόλια" {} + ');
我们会为每个修改过的文件和每行显示不同的行 :
2016-10-17 Mon 21:09:55.0000000000 ./47test.php 2016-10-17 Mon 20:40:30.0000000000 ./places/00testout.txt 2016-10-17 Mon 20:38:57.0000000000 ./rest/47results.php ./47test.php: 22 :-exec grep -HTni "σχόλια" {} + '); ./rest/47results.php: 5 :σχόλια, ιδέες facebook ./rest/47results.php: 6 :σχόλια, ιδέες twitter ./rest/47results.php: 7 :Τα σχόλια σας
每个find
一个,每个grep
结果一个。
正如开头提到的, 如何打印sorting,每个grep
结果只有一行 ?
2016-10-17 Mon 21:09:55 ./47test.php 22 :-exec grep -HTni "σχόλια" {} + '); 2016-10-17 Mon 20:38:57 ./rest/47results.php: 5 :σχόλια, ιδέες facebook 2016-10-17 Mon 20:38:57 ./rest/47results.php: 6 :σχόλια, ιδέες twitter 2016-10-17 Mon 20:38:57 ./rest/47results.php: 7 :Τα σχόλια σας
您可以使用这个find+grep
组合来获得格式化的结果:
while IFS=$'\06' read -r -d '' tf; do sed "s/^/$t /" <(grep -HTni 'σχόλια' "$f") done < <(find . -type f -mmin -10 -not \( -path ./admin -prune \) \ -not \( -path ./people/languages -prune \) \ -not \( -path ./include -prune \) \ -printf '%TY-%Tm-%Td %Ta %TH:%TM:%.2TS\06%p\0')
\06
作为字段分隔符来处理带有空格/换行符的文件名/路径。 \0
(NULL)用于行终止符出于同样的原因。 %.2TS
用于%.2TS
第二个值的小数部分。 sed
用于在grep
输出的行开始处插入日期/时间。 PHP代码:
$cmd = <<<'EOF' export TZ=":Europe/Athens"; \ find . -type f -mmin -10 -not \( -path ./admin -prune \) \ -not \( -path ./people/languages -prune \) \ -not \( -path ./include -prune \) \ -printf '%TY-%Tm-%Td %Ta %TH:%TM:%.2TS\06%p\0' | while IFS=$'\06' read -r -d '' tf; do grep -HTni 'σχόλια' "$f" | sed "s/^/$t /"; done EOF; // var_dump( $cmd ); echo shell_exec($cmd) . "\n";