Can you bulk edit all the files returned in grep?

I want to bulk edit a ton of files that are returned in grep. (I know I better get better).

So if I do:

grep -rnI 'xg_icon-*' 

How do I transfer all these files to vi?

+41
command-line vim grep search sed
Nov 11 '09 at 16:41
source share
6 answers

The easiest way is to return grep only the file names ( -l instead of -n ) that match the pattern. Run this in a subshell and submit the results to Vim.

 vim $(grep -rIl 'xg_icon-*' *) 
+62
Nov 11 '09 at 16:43
source share

A good general solution to this is to use xargs to convert stdout from a process, such as grep, into an argument list.

A la:

 grep -rIl 'xg_icon-*' | xargs vi 
+21
Nov 11 '09 at 16:47
source share

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

+6
Nov 13 '09 at 16:08
source share

You can do this without handling grep output! This will even allow you to jump to the correct line (using the commands :help quickfix , for example :cn or :cw ). So if you are using bash or zsh:

 vim -q & lt (grep foo * .c)
+6
Jul 16 '13 at 21:08
source share

if what you want to edit looks like all the files, then without using vi to do it manually. (although vi can also be scripted), hypothetically, it looks something like this since you never mention that 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 
+1
Nov 11 '09 at 16:49
source share
 vi `grep -l -i findthisword *` 
0
Oct. 19 '16 at 17:52
source share



All Articles