How to pipe a single item from a command output into another command?

jewbix.cube

Basic example using grep (note that grep would not be my only use-case for this):

$ grep -Irl "foo"
path/to/directory/help.js
path/to/directory/config.js
path/to/directory/task.js

Now I want to open the config.js file in Vi. My normal method would be:

$ vi path/to/directory/config.js

which I had to either type by hand with the assistance of tab-completion, or I highlighted the filename from the grep result and copy/pasted it.

But I'd like to be able to Vi the file by just specifying that it was the 2nd result in the grep command. So something like:

$ grep -Irl "foo" | xargs vi 2

Obviously xargs would not work like that, was just an example. But I'm trying to find if there's a way using xargs (or any other utility) to accomplish this and I'm not finding it.

Something that a teammate suggested to me was to use head and tail together, like this:

$ grep -Irl "foo" | tail -n 1 | xargs vi

would retrieve task.js, and

$ grep -Irl "foo" | head -n 2 | tail -n 1 | xargs vi

would retrieve config.js. Wondering if there is a less verbose method.

davidgo

How about

   vi $( grep -IrL "foo" | awk "NR==3" )

This uses AWK to find the appropriate line. The output is then used as a command line argument for vi.

I'm oldschool, so I prefer the equivalent, but slightly frowned upon version-

 vi ` grep -IrL "foo" | awk "NR==3" `

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to pipe single result from output of locate command to another command?

From Dev

How to pipe output of command to another command while also displaying it on the console?

From Dev

How to pipe data to interactive bash script and pipe output to another command?

From Dev

Win10 - How to pipe the output from one command as an argument of another to check file hash?

From Dev

Bash: How to direct output to both stderr and to stdout, to pipe into another command?

From Dev

Pipe output of awk to sed in single command

From Dev

How to pipe output to sh script and pipe that to a command?

From Dev

zsh capture and pipe output to another command

From Dev

How to merge and pipe results from two different commands to single command?

From Dev

Pipe output from one command to another command's non standard input

From Dev

How to start line with command from output of another command

From Dev

How do I pipe the output of a curl command to an environment variable and use it in another curl command?

From Dev

Powershell: Pipe external command output to another external command

From Dev

Powershell: Pipe external command output to another external command

From Dev

Pipe the output of a command to rm command

From Dev

pipe output into a delete command

From Dev

Pipe the output of a command if it is successful

From Dev

pipe output into a delete command

From Dev

pipe command output to convert?

From Dev

How to pipe command output to other commands?

From Dev

How to pipe a command output to multiple programs

From Dev

Perl: How to pipe output of an open command to a file

From Dev

How To Use A Command Output As An Argument of Another Command?

From Dev

How To Use A Command Output As An Argument of Another Command?

From Dev

Reuse output from command 1 in command 2 using a pipe

From Dev

How to send stderr to stdout with a pipe to another command?

From Dev

Pipe from a while loop to a command but execute another command if pipe command fails

From Dev

Python csv reader: how to pipe output to another script using command line

From Dev

Is there a way to pipe the output of one AWS CLI command as the input to another?

Related Related

  1. 1

    How to pipe single result from output of locate command to another command?

  2. 2

    How to pipe output of command to another command while also displaying it on the console?

  3. 3

    How to pipe data to interactive bash script and pipe output to another command?

  4. 4

    Win10 - How to pipe the output from one command as an argument of another to check file hash?

  5. 5

    Bash: How to direct output to both stderr and to stdout, to pipe into another command?

  6. 6

    Pipe output of awk to sed in single command

  7. 7

    How to pipe output to sh script and pipe that to a command?

  8. 8

    zsh capture and pipe output to another command

  9. 9

    How to merge and pipe results from two different commands to single command?

  10. 10

    Pipe output from one command to another command's non standard input

  11. 11

    How to start line with command from output of another command

  12. 12

    How do I pipe the output of a curl command to an environment variable and use it in another curl command?

  13. 13

    Powershell: Pipe external command output to another external command

  14. 14

    Powershell: Pipe external command output to another external command

  15. 15

    Pipe the output of a command to rm command

  16. 16

    pipe output into a delete command

  17. 17

    Pipe the output of a command if it is successful

  18. 18

    pipe output into a delete command

  19. 19

    pipe command output to convert?

  20. 20

    How to pipe command output to other commands?

  21. 21

    How to pipe a command output to multiple programs

  22. 22

    Perl: How to pipe output of an open command to a file

  23. 23

    How To Use A Command Output As An Argument of Another Command?

  24. 24

    How To Use A Command Output As An Argument of Another Command?

  25. 25

    Reuse output from command 1 in command 2 using a pipe

  26. 26

    How to send stderr to stdout with a pipe to another command?

  27. 27

    Pipe from a while loop to a command but execute another command if pipe command fails

  28. 28

    Python csv reader: how to pipe output to another script using command line

  29. 29

    Is there a way to pipe the output of one AWS CLI command as the input to another?

HotTag

Archive