Can you mass edit all files returned in a grep?

SearchVimCommand LineSedGrep

Search Problem Overview


I want to mass-edit a ton of files that are returned in a grep. (I know, I should get better at sed).

So if I do:

grep -rnI 'xg_icon-*'

How do I pipe all of those files into vi?

Search Solutions


Solution 1 - Search

The easiest way is to have grep return just the filenames (-l instead of -n) that match the pattern. Run that in a subshell and feed the results to Vim.

vim $(grep -rIl 'xg_icon-*' *)

Solution 2 - Search

A nice general solution to this is to use xargs to convert a stdout from a process like grep to an argument list.

A la:

grep -rIl 'xg_icon-*' | xargs vi

Solution 3 - Search

if you use vim and the -p option, it will open each file in a tab, and you can switch between them using gt or gT, or even the mouse if you have mouse support in the terminal

Solution 4 - Search

You can do it without any processing of the grep output! This will even enable you to go the the right line (using :help quickfix commands, eg. :cn or :cw). So, if you are using bash or zsh:

vim -q <(grep foo *.c)

Solution 5 - Search

if what you want to edit is similar across all files, then no point using vi to do it manually. (although vi can be scripted as well), hypothetically, it looks something like this, since you never mention what you want to edit

grep -rnI 'xg_icon-*' | while read FILE
do
    sed -i.bak 's/old/new/g' $FILE # (or other editing commands, eg awk... )
done

Solution 6 - Search

vi `grep -l -i findthisword *`

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmagerView Question on Stackoverflow
Solution 1 - SearchjamessanView Answer on Stackoverflow
Solution 2 - SearchBenjView Answer on Stackoverflow
Solution 3 - Searchuser210574View Answer on Stackoverflow
Solution 4 - SearchJaenView Answer on Stackoverflow
Solution 5 - Searchghostdog74View Answer on Stackoverflow
Solution 6 - SearchMookayamaView Answer on Stackoverflow