Bash – How to Open a File Resulting from Grep

bashgrep

I often grep a bunch of files to find a line, and then grep returns one result. Rather than copying and pasting the filename into a new command, I'd like to be able to open that one result with an editor. Something like: grep foo | vim. Is there a way to do that in BASH?

Best Answer

Use grep -l to just get the filename of the matching file and not the matching text, then combine it with vim:

vim "$(grep -l some_pattern file_names)"