133

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.

Jens Erat
  • 18,485
  • 14
  • 68
  • 80
Jonathan Day
  • 1,641

7 Answers7

144

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!

38

The accepted answer doesn't work on the Mac -- as @benroth says, pressing s just moves down a line -- but you can use a different method.

In less --help:

|Xcommand            Pipe file between current pos & mark X to shell command.

and

A mark is any upper-case or lower-case letter.
Certain marks are predefined:
     ^  means  beginning of the file
     $  means  end of the file

So if you go to the top of the buffer (<) and then:

|$cat > /tmp/foo.txt

the contents of the buffer will be written out to /tmp/foo.txt.

Joe Shaw
  • 496
10

When your less is opened, you can save the complete output to a file. Like vim, less supports commands.

Just type the key s, then less will ask you the name of the file where you wish to save the content, just type the file name and then type Enter.

Cheers

1

I wanted to do a slightly different thing: write just the currently visible portion of the text to a file. I found that I could do so using the "|" operator, specifying "." as the mark, and cat >/tmp/mycopy as the shell command.

user54690
  • 196
0

My answer comes a tad too late I believe. But just for reference, in response to benroth's concern above: For OSX users there's always the option to dump the contents of the pager to a log file by using the option "-l" (read DASH ELL) at the colon prompt.

The pager will ask for a log file. Key it and press [CR]

superk
  • 101
0

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
mmmmmm
  • 6,242
-1

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

Dror
  • 1,928