search a string in all the .log files in a directory and move the name of the file which contains the specified string excluding extension .log to a new .txt file
I have tried this but this is wrong
if grep --include=\*.log -rnw '/path/' -e "scp error"
then
do FILENAME=${f%%.*};
echo ${FILENAME} > redrop_files.txt;
done;
Please help
CodePudding user response:
You have one main problem: you don't store the results of grep
anywhere so variable f
is undefined.
It is not clear why you use -n
with grep if you only want the filename; -l
seems to make more sense.
So:
grep --include=\*.log -rlw '/path/' -e "scp error" |\
while read -r f; do
echo "${f%%.*}"
done > redrop_files.txt
CodePudding user response:
Try the following,
path=.
while read file; do
echo ${file%%:*} >> redrop_files.txt
done < <(grep --include=\*.log -rnw $path -e "scp error")