我已经通过管道将命令传递给less,现在我想将命令的输出保存到文件中。我该怎么做?

在这种情况下,我不想使用tee,我希望直接从更少的地方获得解决方案,这样,如果出现以下情况,我不必重新运行长时间运行的命令我忘了使用tee

这个问题与此类似,唯一的区别是我要保存所有行,而不是子集:从less

#1 楼

less中,键入s,然后键入要保存到的文件名,然后按Enter。
man页面的COMMANDS下:

s filename
      Save the input to a file.  This only works if the input is a pipe, not an ordinary file.


man页面还指出,根据您的特定安装,s命令可能不可用。在这种情况下,您可以使用以下代码转到第1行: >
因此:

g or < or ESC-<
      Go to line N in the file, default 1 (beginning of file).


或:

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


ie输入g或<(g或小于)| $(先用管道然后是美元),然后再输入cat和Enter。
无论输入是管道还是普通文件,这都应该起作用。

#2 楼

不使用tee的一种方法如下。 less /path/to/input > /path/to/output
如果已经存在,上面的内容将覆盖/path/to/output

要执行相同的操作,但将其附加到/path/to/output如下。 less /path/to/input >> /path/to/output
要在打开文本后将其保存在less中,可以使用“保存文件”功能。
less中时,请按s。然后,键入文件名。
如果使用管道,则文件将自动写入工作目录。
如果不使用管道,则将打开带有文本的文件(您可以保存)。
我相信文件会在vim中打开。

评论


一旦我打开得更少,就希望得到更少的解决方案。

– Flimm
2014-12-10 21:02

我懂了。很抱歉对于这个误会。我已经更新了答案。

–nehcsivart
2014-12-10 21:10



#3 楼

您不用少使用,可以先使用三通,再使用三通将其减少。

评论


在问题中,我特别要求一种解决方案,该解决方案一旦打开得更少,就无需三通。

– Flimm
16年8月4日在12:39