结合文件修改日期和“grep”结果通过“查找”,在一行中 [英] Combine file modified date and "grep" results through "find", in one line

查看:173
本文介绍了结合文件修改日期和“grep”结果通过“查找”,在一行中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们希望通过 find 命令将 grep 应用于选定文件时显示每个文件的修改日期和时间。最终结果应该如下所示:


2016-10-17周一20:38:57 ./rest/ 47个结果.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周一21:09:55.0000000000 ./47test.php 
2016-10-17 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:对于每一个查找以及每个>每一个 grep 结果。



正如开头提到的,如何打印排序,组合对于每个 grep


  2016-10-17 Mon 21:09:55 ./47test.php 22:-exec grep -HTniσχόλια{} +'); 
2016-10-17周一20:38:57 ./rest/47results.php:5:σχόλια,ιδέεςfacebook
2016-10-17周一20:38:57 ./rest/47results。 php:6:σχόλια,ιδέεςtwitter
2016-10-17周一20:38:57 ./rest/47results.php:7:Τασχόλιασας


解决方案

您可以使用 find + grep b
$ b

 ,而IFS = $'\06'读取-r -d''结果得到格式化结果:

TF;做
seds / ^ / $ t /<(grep -HTni'σχόλια'$ f)
完成< <(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 作为字段分隔符,用空白/由于相同的原因,code>(NULL)被用作行终止符。

  • %。2TS 第二个值的一部分。
  • sed 用于在 grep 输出。






strong>

  $ cmd = <<<'EOF'
export TZ =:Europe / Athens ; \
找到。 - 类型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'|
,而IFS = $'\06'读-r -d''t f;做grep -HTni'σχόλια'$ f| seds / ^ / $ t /;完成
EOF;

// var_dump($ cmd);

echo shell_exec($ cmd)。 \\\
;


We want to show each file's modified date and time when applying grep to selected files by the find command. The final result should look like:

2016-10-17 Mon 20:38:57 ./rest/47results.php: 5 :σχόλια, ιδέες facebook

Running the following from 47test.php file:

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 "σχόλια" {} + ');

we get distinct lines printed for each modified file and each line:

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  :Τα σχόλια σας

One for each find and one for each grep result.

As mentioned in the beginning, how can one print sorted, combined results in just one line for each 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  :Τα σχόλια σας

解决方案

You can use this find+grep combination to get the formatted result:

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')

  • Note use of \06 as field delimiter to address filenames/paths with whitespaces/newlines etc.
  • \0 (NULL) is used as line terminator for the same reason.
  • %.2TS is used to trip fractional part of the second value.
  • sed is used to insert date/time at line start of grep output.

PHP Code:

$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";

这篇关于结合文件修改日期和“grep”结果通过“查找”,在一行中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆