Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

linux - Combine file modified date and "grep" results through "find", in one line

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  :Τα σχ?λια σα?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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) . "
";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...