Wednesday, June 5, 2013

Open all files where grep condition is matched

I wanted to open all of the files in an editor where a specific grep pattern was matched.  This was made easy with the use of grep's recursive search, the cut field (to trim out just the file name), and the open command.

For example, to recursively search all files from the current directory, find all files that contain "implements Serializable", and open them, use the following command:

grep -r "implements Serializable" . | cut -d ":" -f1 | xargs open

This will search all of the files for the text, strip off just the file name (what appears before the colon, ":"), and open using the default editor.

No comments:

Post a Comment