如何重新格式化 BASH 脚本的输出?

How do I reformat the output of a BASH script?

我正在尝试在包含字符串的目录中查找特定文件。

到目前为止我写的代码:

for x in $(find "" -type f -name "*.""") ;
do  
     grep -Hrnw $x -e ""
done

我得到的输出:

./crop.py:2:import torch
./crop.py:3:import torch.backends.cudnn as cudnn

我正在尝试像这样在冒号两边留出空格:

./crop.py : 2 : import torch
./crop.py : 3 : import torch.backends.cudnn as cudnn

我对 BASH 中的编程还很陌生。我试过使用 sed 命令但没有成功。

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'

为什么要使用 for 循环浏览 find 结果,而可以使用 find ... -exec?

像这样:

find "" -type f -name "*.""" -exec grep -Hrnw {} -e "" \;

(我没有测试这个,它可能包含一些错误)

find "" -type f -name "*." | xargs grep -Hrnw -e ""| sed 's/:/ : /g'

建议尝试一种线性命令:

grep -Hrnw "" $(find "" -type f -name "*." -printf "\"%p\"\n") | sed 's/:/ : /g'