我正在尝试打印ls的手册页,我在我的文件中输出重复的字符。我对bash比较新,我不知道从哪里开始这个问题。这是我输入的命令
man ls | cat > file.txt
我期待输出像终端一样
DESCRIPTION
For each operand that names a file of a type other than directory, ls displays its
name as well as any requested, associated information. For each operand that names a
file of type directory, ls displays the names of files contained within that direc-
tory, as well as any requested, associated information.
If no operands are given, the contents of the current directory are displayed. If
more than one operand is given, non-directory operands are displayed first; directory
and non-directory operands are sorted separately and in lexicographical order.
The following options are available:
-@ Display extended attribute keys and sizes in long (-l) output.
-1 (The numeric digit ``one''.) Force output to be one entry per line. This is
the default when output is not to a terminal.
-A List all entries except for . and ... Always set for the super-user.
-a Include directory entries whose names begin with a dot (.).
-B Force printing of non-printable characters (as defined by ctype(3) and cur-
rent locale settings) in file names as \xxx, where xxx is the numeric value
of the character in octal.
-b As -B, but use C escape codes whenever possible.
-C Force multi-column output; this is the default when output is to a terminal.
但是我在文件中输出的内容是这样的
DDEESSCCRRIIPPTTIIOONN
For each operand that names a _f_i_l_e of a type other than directory, llss
displays its name as well as any requested, associated information. For
each operand that names a _f_i_l_e of type directory, llss displays the names
of files contained within that directory, as well as any requested, asso-
ciated information.
If no operands are given, the contents of the current directory are dis-
played. If more than one operand is given, non-directory operands are
displayed first; directory and non-directory operands are sorted sepa-
rately and in lexicographical order.
The following options are available:
--@@ Display extended attribute keys and sizes in long (--ll) output.
--11 (The numeric digit ``one''.) Force output to be one entry per
line. This is the default when output is not to a terminal.
--AA List all entries except for _. and _._.. Always set for the super-
user.
--aa Include directory entries whose names begin with a dot (_.).
--BB Force printing of non-printable characters (as defined by
ctype(3) and current locale settings) in file names as \_x_x_x,
where _x_x_x is the numeric value of the character in octal.
--bb As --BB, but use C escape codes whenever possible.
--CC Force multi-column output; this is the default when output is to
a terminal.
--cc Use time when file status was last changed for sorting (--tt) or
是什么让它做到这一点,我怎么能用可读文本获取手册页?
有些系统有一个man
程序,它注意到它是将输出发送到终端还是管道,并且在每种情况下表现不同。
例如,在ubuntu linux上,man man
有一个选项:
MAN_KEEP_FORMATTING Normally, when output is not being directed to a terminal (such as to a file or a pipe), formatting characters are discarded to make it easier to read the result without special tools. How- ever, if $MAN_KEEP_FORMATTING is set to any non-empty value, these formatting characters are retained. This may be useful for wrappers around man that can interpret formatting charac- ters.
在您的情况下,似乎man在将输出发送到管道时行为不同。
可能有一个选项可以打开您正在寻找的行为,但是从输出中删除不需要的字符可能更简单。一种常见的方法是使用col
:
man ls | col -bx > file.txt