How can I open a file that results from grep? The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsHow to start line with command from output of another commandgrep for files and open the resultant matchesWhat params do I pass to grep to return only file names?Can I have my shell history record how wildcards expanded?Is there a way to get the effect of combining grep -v with grep -A?Script to create files in a templateHow can I grep contents of files with bash only without using find or grep -r?How can I find the program I'm hiding in bashAutocomplete from grep outputQuick way to open results from `find` or `locate`BASH: search and open file from within vimPrinting filename along with grep results in find -exec
I could not break this equation. Please help me
Keeping a retro style to sci-fi spaceships?
What can I do if neighbor is blocking my solar panels intentionally?
Does Parliament hold absolute power in the UK?
First use of “packing” as in carrying a gun
Why is superheterodyning better than direct conversion?
Is above average number of years spent on PhD considered a red flag in future academia or industry positions?
Simulation of a banking system with an Account class in C++
How to delete random line from file using Unix command?
Is this wall load bearing? Blueprints and photos attached
Windows 10: How to Lock (not sleep) laptop on lid close?
Simulating Exploding Dice
Make it rain characters
What is special about square numbers here?
Python - Fishing Simulator
Would an alien lifeform be able to achieve space travel if lacking in vision?
Who or what is the being for whom Being is a question for Heidegger?
Searching for a differential characteristic (differential cryptanalysis)
Take groceries in checked luggage
Can the DM override racial traits?
Sort a list of pairs representing an acyclic, partial automorphism
How did passengers keep warm on sail ships?
Why does the Event Horizon Telescope (EHT) not include telescopes from Africa, Asia or Australia?
Can a 1st-level character have an ability score above 18?
How can I open a file that results from grep?
The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election ResultsHow to start line with command from output of another commandgrep for files and open the resultant matchesWhat params do I pass to grep to return only file names?Can I have my shell history record how wildcards expanded?Is there a way to get the effect of combining grep -v with grep -A?Script to create files in a templateHow can I grep contents of files with bash only without using find or grep -r?How can I find the program I'm hiding in bashAutocomplete from grep outputQuick way to open results from `find` or `locate`BASH: search and open file from within vimPrinting filename along with grep results in find -exec
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
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?
bash grep
add a comment |
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?
bash grep
5
You were rather close, simply tellvim
to read fromSTDIN
:grep foo | vim -
– devnull
Feb 17 '14 at 19:01
2
See also unix.stackexchange.com/q/110146/22565
– Stéphane Chazelas
Feb 17 '14 at 19:49
add a comment |
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?
bash grep
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?
bash grep
bash grep
asked Feb 17 '14 at 18:35
JonathanJonathan
1263628
1263628
5
You were rather close, simply tellvim
to read fromSTDIN
:grep foo | vim -
– devnull
Feb 17 '14 at 19:01
2
See also unix.stackexchange.com/q/110146/22565
– Stéphane Chazelas
Feb 17 '14 at 19:49
add a comment |
5
You were rather close, simply tellvim
to read fromSTDIN
:grep foo | vim -
– devnull
Feb 17 '14 at 19:01
2
See also unix.stackexchange.com/q/110146/22565
– Stéphane Chazelas
Feb 17 '14 at 19:49
5
5
You were rather close, simply tell
vim
to read from STDIN
: grep foo | vim -
– devnull
Feb 17 '14 at 19:01
You were rather close, simply tell
vim
to read from STDIN
: grep foo | vim -
– devnull
Feb 17 '14 at 19:01
2
2
See also unix.stackexchange.com/q/110146/22565
– Stéphane Chazelas
Feb 17 '14 at 19:49
See also unix.stackexchange.com/q/110146/22565
– Stéphane Chazelas
Feb 17 '14 at 19:49
add a comment |
7 Answers
7
active
oldest
votes
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)"
Additionally, you can use-q
flag ofgrep
to make sure grep has indeed resulted in some matching file:grep -q patterns fnames && vim $(grep -l pattern fnames)
– mkc
Feb 17 '14 at 18:45
Should I make a remark about filenames with spaces?
– Bernhard
Feb 17 '14 at 19:08
@Bernhard It can never hurt, and also about those with newlines and other special characters. I recognise the problem whenvi
complains, the OP might not.
– Anthon
Feb 17 '14 at 19:29
add a comment |
You can use quickfix
or errorfile
feature in vim
:
$ grep -n foo * > /tmp/foo.list
$ vim -q /tmp/foo.list
Vim will open the first file in /tmp/foo.list
and place the cursor directly in the line where foo
was found. You can go to the next instance using :cn
and previous instance using :cp
.
Side note: If you are already using vim
or gvim
, then I would suggest using its inbuilt grep
functionality. Read :help vimgrep
for more information.
add a comment |
For search in project no need to leave vim with Fugitive
autocmd QuickFixCmdPost *grep* cwindow
Below you can see example of :Ggrep config
add a comment |
EDIT: After reading your comment, I realize I didn't understand the question initially. Here is a simple script which takes your search term as an argument and prompts for the path to search. If the result is found in only one file, it opens the file for editing at the location of the search result.
#!/bin/bash
SRCHTRM="$1"
read -p "Where to search: " SRCHPATH
FILEFOUND=$(grep "$SRCHTRM" $SRCHPATH)
FILEFOUNDCNT=$(grep "$SRCHTRM" $SRCHPATH | wc -l)
FILEAWK=$(echo "$FILEFOUND" | awk -F":" ' print $1 ')
if [ "$FILEFOUNDCNT" -eq "1" ];then
vi +/"$SRCHTRM" "$FILEAWK"
else
echo "$1 was found in more than one file"
fi
But this seems to allow me to edit the output of grep, but what I want to do is edit the file with the filename that grep returns.
– Jonathan
Feb 17 '14 at 19:24
add a comment |
@devnull got the answer right in this comment:
You were rather close, simply tell vim to read from STDIN:
grep foo | vim -
add a comment |
To expand on Anthon's answer for a case where you have more than one match, it may be helpful in some cases to use head -1
(from this answer) to select only the first result: vim "$(grep -l STRING FILES|head -1)"
add a comment |
If you are performing a long command that has grep somewhere in the middle of it you can also do
vim `command1 | grep whatever | awk 'do_something_else'`
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f115591%2fhow-can-i-open-a-file-that-results-from-grep%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
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)"
Additionally, you can use-q
flag ofgrep
to make sure grep has indeed resulted in some matching file:grep -q patterns fnames && vim $(grep -l pattern fnames)
– mkc
Feb 17 '14 at 18:45
Should I make a remark about filenames with spaces?
– Bernhard
Feb 17 '14 at 19:08
@Bernhard It can never hurt, and also about those with newlines and other special characters. I recognise the problem whenvi
complains, the OP might not.
– Anthon
Feb 17 '14 at 19:29
add a comment |
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)"
Additionally, you can use-q
flag ofgrep
to make sure grep has indeed resulted in some matching file:grep -q patterns fnames && vim $(grep -l pattern fnames)
– mkc
Feb 17 '14 at 18:45
Should I make a remark about filenames with spaces?
– Bernhard
Feb 17 '14 at 19:08
@Bernhard It can never hurt, and also about those with newlines and other special characters. I recognise the problem whenvi
complains, the OP might not.
– Anthon
Feb 17 '14 at 19:29
add a comment |
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)"
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)"
edited Feb 17 '14 at 19:31
answered Feb 17 '14 at 18:39
AnthonAnthon
61.7k17107171
61.7k17107171
Additionally, you can use-q
flag ofgrep
to make sure grep has indeed resulted in some matching file:grep -q patterns fnames && vim $(grep -l pattern fnames)
– mkc
Feb 17 '14 at 18:45
Should I make a remark about filenames with spaces?
– Bernhard
Feb 17 '14 at 19:08
@Bernhard It can never hurt, and also about those with newlines and other special characters. I recognise the problem whenvi
complains, the OP might not.
– Anthon
Feb 17 '14 at 19:29
add a comment |
Additionally, you can use-q
flag ofgrep
to make sure grep has indeed resulted in some matching file:grep -q patterns fnames && vim $(grep -l pattern fnames)
– mkc
Feb 17 '14 at 18:45
Should I make a remark about filenames with spaces?
– Bernhard
Feb 17 '14 at 19:08
@Bernhard It can never hurt, and also about those with newlines and other special characters. I recognise the problem whenvi
complains, the OP might not.
– Anthon
Feb 17 '14 at 19:29
Additionally, you can use
-q
flag of grep
to make sure grep has indeed resulted in some matching file: grep -q patterns fnames && vim $(grep -l pattern fnames)
– mkc
Feb 17 '14 at 18:45
Additionally, you can use
-q
flag of grep
to make sure grep has indeed resulted in some matching file: grep -q patterns fnames && vim $(grep -l pattern fnames)
– mkc
Feb 17 '14 at 18:45
Should I make a remark about filenames with spaces?
– Bernhard
Feb 17 '14 at 19:08
Should I make a remark about filenames with spaces?
– Bernhard
Feb 17 '14 at 19:08
@Bernhard It can never hurt, and also about those with newlines and other special characters. I recognise the problem when
vi
complains, the OP might not.– Anthon
Feb 17 '14 at 19:29
@Bernhard It can never hurt, and also about those with newlines and other special characters. I recognise the problem when
vi
complains, the OP might not.– Anthon
Feb 17 '14 at 19:29
add a comment |
You can use quickfix
or errorfile
feature in vim
:
$ grep -n foo * > /tmp/foo.list
$ vim -q /tmp/foo.list
Vim will open the first file in /tmp/foo.list
and place the cursor directly in the line where foo
was found. You can go to the next instance using :cn
and previous instance using :cp
.
Side note: If you are already using vim
or gvim
, then I would suggest using its inbuilt grep
functionality. Read :help vimgrep
for more information.
add a comment |
You can use quickfix
or errorfile
feature in vim
:
$ grep -n foo * > /tmp/foo.list
$ vim -q /tmp/foo.list
Vim will open the first file in /tmp/foo.list
and place the cursor directly in the line where foo
was found. You can go to the next instance using :cn
and previous instance using :cp
.
Side note: If you are already using vim
or gvim
, then I would suggest using its inbuilt grep
functionality. Read :help vimgrep
for more information.
add a comment |
You can use quickfix
or errorfile
feature in vim
:
$ grep -n foo * > /tmp/foo.list
$ vim -q /tmp/foo.list
Vim will open the first file in /tmp/foo.list
and place the cursor directly in the line where foo
was found. You can go to the next instance using :cn
and previous instance using :cp
.
Side note: If you are already using vim
or gvim
, then I would suggest using its inbuilt grep
functionality. Read :help vimgrep
for more information.
You can use quickfix
or errorfile
feature in vim
:
$ grep -n foo * > /tmp/foo.list
$ vim -q /tmp/foo.list
Vim will open the first file in /tmp/foo.list
and place the cursor directly in the line where foo
was found. You can go to the next instance using :cn
and previous instance using :cp
.
Side note: If you are already using vim
or gvim
, then I would suggest using its inbuilt grep
functionality. Read :help vimgrep
for more information.
answered Oct 5 '14 at 15:22
SrinidhiSrinidhi
41625
41625
add a comment |
add a comment |
For search in project no need to leave vim with Fugitive
autocmd QuickFixCmdPost *grep* cwindow
Below you can see example of :Ggrep config
add a comment |
For search in project no need to leave vim with Fugitive
autocmd QuickFixCmdPost *grep* cwindow
Below you can see example of :Ggrep config
add a comment |
For search in project no need to leave vim with Fugitive
autocmd QuickFixCmdPost *grep* cwindow
Below you can see example of :Ggrep config
For search in project no need to leave vim with Fugitive
autocmd QuickFixCmdPost *grep* cwindow
Below you can see example of :Ggrep config
answered Jul 15 '15 at 7:04
goodnicewebgoodniceweb
111
111
add a comment |
add a comment |
EDIT: After reading your comment, I realize I didn't understand the question initially. Here is a simple script which takes your search term as an argument and prompts for the path to search. If the result is found in only one file, it opens the file for editing at the location of the search result.
#!/bin/bash
SRCHTRM="$1"
read -p "Where to search: " SRCHPATH
FILEFOUND=$(grep "$SRCHTRM" $SRCHPATH)
FILEFOUNDCNT=$(grep "$SRCHTRM" $SRCHPATH | wc -l)
FILEAWK=$(echo "$FILEFOUND" | awk -F":" ' print $1 ')
if [ "$FILEFOUNDCNT" -eq "1" ];then
vi +/"$SRCHTRM" "$FILEAWK"
else
echo "$1 was found in more than one file"
fi
But this seems to allow me to edit the output of grep, but what I want to do is edit the file with the filename that grep returns.
– Jonathan
Feb 17 '14 at 19:24
add a comment |
EDIT: After reading your comment, I realize I didn't understand the question initially. Here is a simple script which takes your search term as an argument and prompts for the path to search. If the result is found in only one file, it opens the file for editing at the location of the search result.
#!/bin/bash
SRCHTRM="$1"
read -p "Where to search: " SRCHPATH
FILEFOUND=$(grep "$SRCHTRM" $SRCHPATH)
FILEFOUNDCNT=$(grep "$SRCHTRM" $SRCHPATH | wc -l)
FILEAWK=$(echo "$FILEFOUND" | awk -F":" ' print $1 ')
if [ "$FILEFOUNDCNT" -eq "1" ];then
vi +/"$SRCHTRM" "$FILEAWK"
else
echo "$1 was found in more than one file"
fi
But this seems to allow me to edit the output of grep, but what I want to do is edit the file with the filename that grep returns.
– Jonathan
Feb 17 '14 at 19:24
add a comment |
EDIT: After reading your comment, I realize I didn't understand the question initially. Here is a simple script which takes your search term as an argument and prompts for the path to search. If the result is found in only one file, it opens the file for editing at the location of the search result.
#!/bin/bash
SRCHTRM="$1"
read -p "Where to search: " SRCHPATH
FILEFOUND=$(grep "$SRCHTRM" $SRCHPATH)
FILEFOUNDCNT=$(grep "$SRCHTRM" $SRCHPATH | wc -l)
FILEAWK=$(echo "$FILEFOUND" | awk -F":" ' print $1 ')
if [ "$FILEFOUNDCNT" -eq "1" ];then
vi +/"$SRCHTRM" "$FILEAWK"
else
echo "$1 was found in more than one file"
fi
EDIT: After reading your comment, I realize I didn't understand the question initially. Here is a simple script which takes your search term as an argument and prompts for the path to search. If the result is found in only one file, it opens the file for editing at the location of the search result.
#!/bin/bash
SRCHTRM="$1"
read -p "Where to search: " SRCHPATH
FILEFOUND=$(grep "$SRCHTRM" $SRCHPATH)
FILEFOUNDCNT=$(grep "$SRCHTRM" $SRCHPATH | wc -l)
FILEAWK=$(echo "$FILEFOUND" | awk -F":" ' print $1 ')
if [ "$FILEFOUNDCNT" -eq "1" ];then
vi +/"$SRCHTRM" "$FILEAWK"
else
echo "$1 was found in more than one file"
fi
edited Feb 17 '14 at 23:25
answered Feb 17 '14 at 19:09
Timothy MartinTimothy Martin
5,4342430
5,4342430
But this seems to allow me to edit the output of grep, but what I want to do is edit the file with the filename that grep returns.
– Jonathan
Feb 17 '14 at 19:24
add a comment |
But this seems to allow me to edit the output of grep, but what I want to do is edit the file with the filename that grep returns.
– Jonathan
Feb 17 '14 at 19:24
But this seems to allow me to edit the output of grep, but what I want to do is edit the file with the filename that grep returns.
– Jonathan
Feb 17 '14 at 19:24
But this seems to allow me to edit the output of grep, but what I want to do is edit the file with the filename that grep returns.
– Jonathan
Feb 17 '14 at 19:24
add a comment |
@devnull got the answer right in this comment:
You were rather close, simply tell vim to read from STDIN:
grep foo | vim -
add a comment |
@devnull got the answer right in this comment:
You were rather close, simply tell vim to read from STDIN:
grep foo | vim -
add a comment |
@devnull got the answer right in this comment:
You were rather close, simply tell vim to read from STDIN:
grep foo | vim -
@devnull got the answer right in this comment:
You were rather close, simply tell vim to read from STDIN:
grep foo | vim -
answered Oct 19 '16 at 16:53
NoumenonNoumenon
1013
1013
add a comment |
add a comment |
To expand on Anthon's answer for a case where you have more than one match, it may be helpful in some cases to use head -1
(from this answer) to select only the first result: vim "$(grep -l STRING FILES|head -1)"
add a comment |
To expand on Anthon's answer for a case where you have more than one match, it may be helpful in some cases to use head -1
(from this answer) to select only the first result: vim "$(grep -l STRING FILES|head -1)"
add a comment |
To expand on Anthon's answer for a case where you have more than one match, it may be helpful in some cases to use head -1
(from this answer) to select only the first result: vim "$(grep -l STRING FILES|head -1)"
To expand on Anthon's answer for a case where you have more than one match, it may be helpful in some cases to use head -1
(from this answer) to select only the first result: vim "$(grep -l STRING FILES|head -1)"
answered Aug 8 '17 at 22:25
EL_DONEL_DON
1114
1114
add a comment |
add a comment |
If you are performing a long command that has grep somewhere in the middle of it you can also do
vim `command1 | grep whatever | awk 'do_something_else'`
add a comment |
If you are performing a long command that has grep somewhere in the middle of it you can also do
vim `command1 | grep whatever | awk 'do_something_else'`
add a comment |
If you are performing a long command that has grep somewhere in the middle of it you can also do
vim `command1 | grep whatever | awk 'do_something_else'`
If you are performing a long command that has grep somewhere in the middle of it you can also do
vim `command1 | grep whatever | awk 'do_something_else'`
answered yesterday
GregGreg
1289
1289
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f115591%2fhow-can-i-open-a-file-that-results-from-grep%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
-bash, grep
5
You were rather close, simply tell
vim
to read fromSTDIN
:grep foo | vim -
– devnull
Feb 17 '14 at 19:01
2
See also unix.stackexchange.com/q/110146/22565
– Stéphane Chazelas
Feb 17 '14 at 19:49