1

On bash, I have the following (typical) scenario:

~/some/dir$ find | grep java

which outputs, for example

./subdir1/subdir2/file1.java
./subdir1/subdir3/file2.java
./subdir1/subdir4/file3.java
./subdir1/subdir2/file4.java
./subdir1/subdir6/file5.java

now I know that file5.java, which is the file I was looking for (for example), is on that subdir, so I execute:

vi subdir1/subdir6/file5.java

but in order to do this I have to either type the path (I know that using the TAB key speeds it up) or copy and paste the path to the file

the question is: is there a shortcut/variable on bash (or any other shell) that references the lines outputted by the previous command, so that I can say

vi [whatever the previous command returned on the 5th line]

, and that would be a nice time saver

thanks!

AndresQ
  • 803
  • 5
  • 19
  • 1
    would vi $( find | grep java | head -5 | tail -1 ) be too complicated? – Dima Chubarov Apr 27 '12 at 17:44
  • the problem is that I can't know before I execute the find on which line is the file that I want to edit, so it has to be on two different commands – AndresQ Apr 27 '12 at 17:46
  • related: http://stackoverflow.com/questions/5955577/bash-automatically-capture-output-of-last-executed-command-into-a-variable – Nate Kohl Apr 27 '12 at 18:32

3 Answers3

2

With BASH history you can come close to [whatever the previous command returned on line 5]

Here is an example

 $ find -name "*.java"
 src/file1.java
 ...
 src/file5.java
 ...
 $ vi $( !! | head -5 | tail -1 )

In this example !! represents [whatever the previous command returned] in the sence that the command is executed again.

The following command | head -5 | tail -1 is a rather awkward way to say [take line 5] from the input

If you are after a particular filename you could as well specify that in the search expression as

$ vi $( !find | grep file5 )

If find takes a long time to execute you could save the output of find into a file and use that file instead.

$ find -name "*.java" | tee filelist.save
 src/file1.java
 ...
 src/file5.java
 ...
 $ vi $( grep file5 filelist.save )
Dima Chubarov
  • 16,199
  • 6
  • 40
  • 76
  • I was hoping for a magic shorcut such as `vi €5`, but this one is fine! I'll use it with !! instead of !find (to re-execute the last command) and maybe write a little script to reduce keystrokes – AndresQ Apr 27 '12 at 18:12
  • 1
    `!!` will repeat whatever the last command was, if you're interested in making this slightly more general. – Nate Kohl Apr 27 '12 at 18:31
1

add -n 5 in your grep command will do just that.

~/some/dir$ find | grep -n 5 java

johnshen64
  • 3,734
  • 1
  • 21
  • 17
  • the problem is that I can't know before I execute the find on which line is the file that I want to edit, so it has to be on two different commands – AndresQ Apr 27 '12 at 17:50
  • 1
    ah, but someone already answered that, you can use a subshell such as vi $(find | grep -n 5 java). – johnshen64 Apr 27 '12 at 18:02
0

This tool is exactly what I needed:

http://facebook.github.io/PathPicker/

AndresQ
  • 803
  • 5
  • 19