Перенаправление man в файл не совпадает с текстом в консоли - PullRequest
0 голосов
/ 13 февраля 2019

Я пытаюсь распечатать справочную страницу для 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

Что заставило бы это сделать это и как я могу получить справочную страницу в читаемом тексте?

1 Ответ

0 голосов
/ 13 февраля 2019

В некоторых системах есть программа 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.

В вашем случае, похоже, что человек не ведет себя по-разному при отправке вывода в канал.

Возможноможно включить нужное вам поведение, но может быть проще просто удалить нежелательные символы из вывода.Распространенным методом является использование col:

man ls | col -bx > file.txt
...