当grep通过find命令应用于选定文件时,我们希望显示每个文件的修改日期和时间。最终结果应如下所示:
grep
find
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结果。
如开头所提到的, 如何将一张印刷品排序,合并的结果每行仅一行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 :Τα σχόλια σας
2016-10-17 Mon 21:09:55 ./47test.php 22 :-exec grep -HTni "σχόλια" {}
您可以使用此find+grep组合来获取格式化结果:
find+grep
while IFS=$'\06' read -r -d '' t f; 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
%.2TS
sed
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 '' t f; do grep -HTni 'σχόλια' "$f" | sed "s/^/$t /"; done EOF; // var_dump( $cmd ); echo shell_exec($cmd) . "\n";