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

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

问题描述

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

<块引用>

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

我们会为每个修改过的文件和每一行打印不同的行:

2016-10-17 Mon 21:09:55.0000000000 ./47test.php2016-10-17 星期一 20:40:30.0000000000 ./places/00testout.txt2016-10-17 星期一 20:38:57.0000000000 ./rest/47results.php./47test.php: 22 :-exec grep -HTni "σχόλια" {} + ');./rest/47results.php: 5 :σχόλια, ιδέες facebook./rest/47results.php: 6 :σχόλια, ιδέες 推特./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 :σχόλια, ιδέες facebook2016-10-17 Mon 20:38:57 ./rest/47results.php: 6 :σχόλια, ιδέες twitter2016-10-17 Mon 20:38:57 ./rest/47results.php: 7 :Τα σχόλια σας

解决方案

你可以使用这个find+grep组合来得到格式化的结果:

while IFS=$'6' read -r -d '' t f;做sed "s/^/$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:%.2TS6%p')

  • 注意使用 6 作为字段分隔符来处理带有空格/换行符等的文件名/路径
  • (NULL) 出于同样的原因被用作行终止符.
  • %.2TS 用于触发第二个值的小数部分.
  • sed 用于在 grep 输出的行首插入日期/时间.
<小时>

PHP 代码:

$cmd = <<<'EOF'出口 TZ=":欧洲/雅典";寻找 .-type f -mmin -10 -not ( -path ./admin -prune ) -not ( -path ./people/languages -prune ) -not ( -path ./include -prune ) -printf '%TY-%Tm-%Td %Ta %TH:%TM:%.2TS6%p' |而 IFS=$'6' 读取 -r -d '' t f;做 grep -HTni 'σχόλια' "$f" |sed "s/^/$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
" 
-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=$'6' 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:%.2TS6%p')

  • Note use of 6 as field delimiter to address filenames/paths with whitespaces/newlines etc.
  • (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:%.2TS6%p' |
while IFS=$'6' read -r -d '' t f; do grep -HTni 'σχόλια' "$f" | sed "s/^/$t /"; done
EOF;

// var_dump( $cmd );

echo shell_exec($cmd) . "
";

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

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