Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a file using vim from the output of pipe command

Tags:

vim

sed

I have a list of files in a text file abc.txt . I have to read the nth line from the file and open the file using vim . I have done this but the file at nth line doesn't open :-

sed -n 4p abc.txt | vim -

Trying to get 4th line from abc.txt and opening it using vim .But the output I get is content of file at that particular line number :- enter image description here

like image 806
Newbie Avatar asked Oct 20 '25 04:10

Newbie


2 Answers

The right command would be like this:

vim "$(sed -n 4p abc.txt)"

The difference is that this passes the output of sed as the first argument to vim. As a result, Vim will open that file.

In the command you've typed, the output of sed is piped to the standard input of vim. Since you're passing '-' as the argument to Vim, it assumes that the text to edit is what is coming through the standard input. This text is the filename, but not the contents of the file.

like image 175
svsd Avatar answered Oct 24 '25 21:10

svsd


Yes I got it . The command is

vim `sed -n 4p abc.txt`
like image 31
Newbie Avatar answered Oct 24 '25 21:10

Newbie