This question already has an answer here:

Every so often I need to take a subset of a log file and save it into a file for later use. I use less a lot to view and search within the logs and for exporting the interesting part, I currently do the following:

  1. in less show the line numbers and note down the line range I need
  2. back on the cli, using sed I extract the range I need and save this into a file

Is it possible to do this from within less, i.e. without the sed part?

marked as duplicate by Ramesh, Anthon, slm Sep 5 '14 at 16:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


Note, the following command may or may not be valid, depending on your particular installation.

From man less.

   | <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.

So when opened a file in less, navigate to the desired position and type the following:

|. cat >filename

|. causes less to pipe the current screen to the given command. The command is cat >filename. cat reads from STDIN and redirects to filename.

You can also open the part within an editor if you want:

|. vi -

The -causes vi to read from STDIN. Then in vi edit whatever you want to edit and save the file with :w filename. You can also use another editor, that can read from STDIN.

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