I am trying to find specific files in a directory that contain a string.
Code I've written so far:
for x in $(find "$1" -type f -name "*."$2"") ;
do
grep -Hrnw $x -e "$3"
done
The output I get:
./crop.py:2:import torch
./crop.py:3:import torch.backends.cudnn as cudnn
I am trying to get spaces on both sides of the colon like this:
./crop.py : 2 : import torch
./crop.py : 3 : import torch.backends.cudnn as cudnn
I am fairly new to programing in BASH. I've tried using sed
command but had not luck with it.
CodePudding user response:
I am trying to get spaces on both sides of the colon
I've tried using sed command but had not luck with it.
sed 's/:/ : /g'
CodePudding user response:
Why are you using a for
-loop for browsing through the find
results, while you can use a find ... -exec
?
Like this:
find "$1" -type f -name "*."$2"" -exec grep -Hrnw {} -e "$3" \;
(I didn't test this, it might contain some bugs)
CodePudding user response:
Suggesting to try one liner command:
grep -Hrnw "$3" $(find "$1" -type f -name "*.$2") | sed 's/:/ : /g'