If I've piped the results of a command to less and then decided that I want to save the contents to a file, is this possible?

I've tried setting a mark a at the end of the buffer, and then returning to the top and using |avi to send the whole contents to vi, but that doesn't work.

link|improve this question

feedback

4 Answers

up vote 7 down vote accepted

On my system, man less says

       s filename
              Save the input to a file.  This only works if  the  input  is  a
              pipe, not an ordinary file.

Works for me!

link|improve this answer
nice, thanks! Works a treat. – Jonathan Day May 31 '11 at 11:43
feedback

Your mark a would be at the top of the last page in less. Use $ for end of file.

| <m> shell-command

<m> represents any mark letter. Pipes a section of the input file to the given shell command. The section of the file to be piped is between the first line on the current screen and the position marked by the letter. <m> may also be ^ or $ to indicate beginning or end of file respectively. If <m> is . or newline, the current screen is piped.

(see man less | less -p '^\s*\|')

Example:

g|$

(Go to top, pipe, to end of file.)

And at the ! (shell-command) prompt, enter:

vim -
link|improve this answer
so I was on the right track with the |avi at least! Thanks. – Jonathan Day May 31 '11 at 11:45
feedback

Use the > operator. For example: less foo.bar > output.txt.

link|improve this answer
Thanks @Dror, but I'm already in the less application, not at the bash prompt any more – Jonathan Day May 31 '11 at 9:53
feedback

No if you have started less, but if you know before yu want to send it to less and a file then you can use the tee command

command | tee out_file | less
link|improve this answer
Thanks Mark, but I'm specifically looking how to do it if I'm already in less – Jonathan Day May 31 '11 at 9:53
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.