Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you mass edit all files returned in a grep?

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?

like image 527
mager Avatar asked Nov 11 '09 16:11

mager


People also ask

Can grep modify files?

Search and edit text files by using the following commands. grep: The grep command allows you to search inside a number of files for a particular search pattern and then print matching lines.

How do you grep everything in a file?

The grep command searches through the file, looking for matches to the pattern specified. To use it type grep , then the pattern we're searching for and finally the name of the file (or files) we're searching in. The output is the three lines in the file that contain the letters 'not'.

What does grep do again?

grep searches input files for lines containing a match to a given pattern list. When it finds a match in a line, it copies the line to standard output (by default), or produces whatever other sort of output you have requested with options.

How do I append grep?

If you wish to append the output at the end of the file, use >> rather than > as the redirection operator. What this actually does is to start cat and grep concurrently. cat will read from q1. txt and try to write it to its standard output, which is connected to the standard input of grep .


2 Answers

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-*' *) 
like image 101
jamessan Avatar answered Sep 27 '22 02:09

jamessan


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 
like image 24
Benj Avatar answered Sep 25 '22 02:09

Benj