Score:0

Detect multiple files at subfolder containing specific string on Linux

ir flag

I have multiple files of work.log at different subfolders need to detect two keyword Can't find file or Not pass by number

Direct path like :

/folder/Date/Job1/20190116_ADMtest_1/ADM.log
/folder/Date/Job1/20190122_ADMtest_2/ADM.log
/folder/Date/Job1/20190203_ADMtest_4/ADM.log
/folder/Date/Job1/20190217_ADMtest_3/ADM.log
/folder/Date/Job1/20190218_ADMtest_21/ADM.log
/folder/Date/Job1/20190225_ADMtest_18/ADM.log

In every log will look like:

[Message] 2019-01-16 11:13:21      Read 2 files from: 20190116_ADMtest_1txt
[Message] 2019-01-16 11:13:21
[Message] 2019-01-16 11:13:21      FATAL ERROR:Can't find file
[ERROR] 2019-01-16 11:13:21     ERROR Message : Stop

or like

[Message] 2019-01-22 10:56:06      #    LOG      1 867 | Runtime: 0 hours 15 minutes and 45 seconds.
[Message] 2019-01-22 10:56:06      #    Message     1 867 |
[Message] 2019-01-22 10:56:06      #    Message     2 152 |
[Message] 2019-01-22 10:56:06      Average number: 58.534678
[ERROR] 2019-01-22 10:56:06     The file failed average number: Average number = 58.534678

I want to get a list to know which subfolders have ErrorI Can't find file and which subfolders have ErrorII Not pass by number

Desire Output like :

ErrorI.txt
20190116_ADMtest_1 
20190203_ADMtest_4
20190225_ADMtest_18

ErrorII.txt
20190122_ADMtest_2
20190217_ADMtest_3
20190218_ADMtest_21
FedKad avatar
cn flag
Something like `grep -l "msg" /folder/Date/Job1/*/ADM.log`?
Raffa avatar
jp flag
... or recursively on the parent directory e.g. `grep -rl 'message' /folder/Date/Job1/`
terdon avatar
cn flag
Can you add an example of the second error? I don't see `Not pass by number` anywhere in your examples. And what should happen if a file has both errors? Should it be listed twice? Will all files be named `ADM.log`?
Score:3
cn flag

Here's a script that searches for the two strings you mention (note that the second string isn't actually present in your examples) and creates two output files, one for each error, with the list of matching directories:

## Delete any already existing output files
rm error1.txt error2.txt 2>/dev/null; 

## define the error strings to search for
error1="Can't find file"
error2="Not pass by number"

## Iterate over all AMP.log files
for f in /folder/Date/Job1/*/AMP.log; do 
 if grep -q "$error1" "$f"; then 
  printf '%s\n' "${f%/*}" >> error1.txt
 elif grep -q "$error2" "$f"; then 
  printf '%s\n' "${f%/*}" >> error2.txt 
 fi
done 

I tested using these example input files:

$ for f in folder/Date/Job1/20190*/*log; do echo "===== $f ===="; cat "$f"; done
===== folder/Date/Job1/20190116_ADMtest_1/AMP.log ====
Can't find file
===== folder/Date/Job1/20190122_ADMtest_2/AMP.log ====
Not pass by number
===== folder/Date/Job1/20190203_ADMtest_4/AMP.log ====
Not pass by number
===== folder/Date/Job1/20190217_ADMtest_3/AMP.log ====
Can't find file
===== folder/Date/Job1/20190218_ADMtest_21/AMP.log ====
Can't find file
===== folder/Date/Job1/20190225_ADMtest_18/AMP.log ====
Not pass by number

And got the following two output files:

$ cat error1.txt 
/folder/Date/Job1/20190116_ADMtest_1
/folder/Date/Job1/20190217_ADMtest_3
/folder/Date/Job1/20190218_ADMtest_21

$ cat error2.txt 
/folder/Date/Job1/20190122_ADMtest_2
/folder/Date/Job1/20190203_ADMtest_4
/folder/Date/Job1/20190225_ADMtest_18
Ollie avatar
ir flag
It's work well!!!! Thank you so much.
Score:1
jp flag

In bash ... In as automatic and as efficient (hard combination) as I could think of at the moment ... Maybe this is what you need:

{
declare -A e=( [ErrorI]="Can't find file" [ErrorII]="Not pass by number" )

for i in "${!e[@]}"; do
  grep -rlm1 "${e[$i]}" /folder/Date/Job1/ |
  awk -F'/' '{print $(NF-1)}' > "${i}.txt"
  done
}

It will create two files: ErrorI.txt and ErrorII.txt in the current working directory with content like:

$ cat ErrorI.txt
20190116_ADMtest_1 
20190203_ADMtest_4
20190225_ADMtest_18

$ cat ErrorII.txt
20190122_ADMtest_2
20190217_ADMtest_3
20190218_ADMtest_21
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.