gpt4 book ai didi

linux - 将文件修改日期和 "grep"结果通过 "find"组合在一行中

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:09:25 26 4
gpt4 key购买 nike

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

最佳答案

您可以使用此 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 (NULL) 出于同样的原因被用作行终止符。
  • %.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 '' t f; do grep -HTni 'σχόλια' "$f" | sed "s/^/$t /"; done
EOF;

// var_dump( $cmd );

echo shell_exec($cmd) . "\n";

关于linux - 将文件修改日期和 "grep"结果通过 "find"组合在一行中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40053819/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com