bash - How do I take the output of recursive grep and split it into an array like variable? -


i'm trying create shell search , replace function replace occurrences of string in directory. problem how take output of recursive grep , use find files use sed?

i've got following pipeline grep -r protal ./ | sed 's/:.*//g' | sed 's/\/\//\//g'. produces output:

./settings.py ./settings.py ./settings.py ./urls.py ./wsgi.py ./wsgi.py 

i want take , split array or can (pseudo-code):

for [[ $file in $file_list ]];    sed -i 's/$input_string/$replace_value/g' $file done 

how that?

instead of trying parse file list (which problem when have whitespaces or newlines in file names), i'd whole thing find , -exec filter:

find . -type f -exec grep -q protal '{}' \; -exec sed -i "s/$input_string/$replace_value/" '{}' \; 

the trick that

  • the -exec filter passes if command returns exit status 0,
  • that grep -q protal file.txt returns exit status 0 if file.txt contains protal, , that
  • the second -exec filter, since chained after first, attempted (and associated command executed) if first filter passes.

this has effect of running sed -i "s/$input_string/$replace_value/" regular files under current directory contain protal.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -