Friday, May 6, 2022

[SOLVED] How do I reformat the output of a BASH script?

Issue

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.


Solution

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


Answered By - ufopilot
Answer Checked By - Mildred Charles (WPSolving Admin)