4

Related to: (grep inside of less) grep inside less?

Related to: (save less to file) https://superuser.com/questions/290908/how-can-i-save-the-current-contents-of-less-to-a-file

When I filter the results typing &<regex> and then save the file contains the unfiltered results. Is it possible to save just the filtered results.

1 Answers1

0

After browsing through man less, I spotted the following options:

-ofilename or --log-file=filename
              Causes less to copy its input to the named file as it is being viewed.  This applies only when the input file is a pipe, not an ordinary file.  If the file already exists, less will  ask  for
              confirmation before overwriting it.

       -Ofilename or --LOG-FILE=filename
              The -O option is like -o, but it will overwrite an existing file without asking for confirmation.

              If  no  log file has been specified, the -o and -O options can be used from within less to specify a log file.  Without a file name, they will simply report the name of the log file.  The "s"
              command is equivalent to specifying -o from within less.

So if you do something like cat big_file.txt | less --log-file=output.txt, you will get the output of less in the output file as you view it.

  • Somehow this option does not work. Log file ends up with the same content as the original file. Checked this on version dpoliaru@host:~$ less --version less 487 (GNU regular expressions) – dmytro.poliarush Mar 03 '20 at 07:37