6

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?

Ketan
  • 9,226
fduff
  • 5,035

1 Answers1

4

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.

chaos
  • 48,171