使用“ less”命令时,如何将终端上显示的内容重定向到文件?


19

如何将终端内容(使用less命令时当前正在查看的内容不是整个文件)重定向到外文件中,直到按Q退出less


你的意思是cat foo | less -o bar这里bar是输出文件和foo输入文件?
Helio 2015年

Answers:


29

要仅保存终端中当前显示的部分,可以使用|命令。

来自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.
  1. 所以第一种|(管道符号)
  2. 然后选择.标记以仅选择终端上可见的内容(或点击Enter
  3. 使用tee保存到一个文件,例如:tee /tmp/section_of_big_file.txt
  4. Enter然后按q

屏幕截图的顺序:

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.