4

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

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

it says Illegal variable name.
Looks like either you have an older version of Bash or you're using some other shell. $(...) is a new and improved version of backticks (`...`), but some shells will not support it. As you rightly figured out, using backticks works on your shell.
Not relevant for the OP's case, but for anyone else who wants to open more than one file at once: vim -p "$(your command)" opens each file in a new vim tab: vim.wikia.com/wiki/Using_tab_pages
1

Yes I got it . The command is

vim `sed -n 4p abc.txt`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.