Grep: Return a user message when no hits
I have ASCII files to parse that 48 hours old or more ; I can identify them like so
find . -name "FILE*TXT" -mtime +1 -exec ls -ltas '{}' ';'
Some files have a list of hardware errors (we test electronic components), some have none. If the file name has no errors, I still want to display a message like so
grep ^err R*VER && echo "No error"
FILEA.TXT:err ->USB 3910 err
FILED.TXT:err No Error
This grep statement works but it seemingly overrides the find() statement above if I run both at the same time... How can I combine the two statements to create a report that lists the filename and error(s) like so
FILEA.TXT Button 3320 err
FILEB.TXT USB 3235 err
FILEC.TXT IR Remote 2436 err
FILED.TXT No error
Is it possible to return "No error" with the file name without error? Thanks in advance for you开发者_Go百科r help.
The -exec
predicate returns false if the command returns failure.
find ... \( -exec grep ... {} \; -o -printf "%p No error\n" \) ...
A friend came up with this
find . -name "FILE*TXT" -mtime +1 -exec sh -c 'grep -H ^err "{}" || echo "{}:No error"' \;
I could not figure out how to run grep for each file ; his solution does exactly that.
Thanks for taking the time.
精彩评论