Linux 使用 'less' 命令时在 Unix 中显示特殊字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6943928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:28:00  来源:igfitidea点击:

Show special characters in Unix while using 'less' Command

linuxunixcommand

提问by Kingsly

I would like to know how to view special characters while using 'less' command. For instance I want to see the non-printable characters with a special notation. For instance in 'vi' editor I use "set list on" to see the line termination characters represented by dollar '$' character. Similarly I would want to do this using 'less' command.

我想知道如何在使用“less”命令时查看特殊字符。例如,我想用特殊符号查看不可打印的字符。例如,在“vi”编辑器中,我使用“set list on”来查看由美元“$”字符表示的行终止字符。同样,我想使用“less”命令来做到这一点。

I referred Unix less manual, but to no avail.

我参考了 Unix less manual,但无济于事。

采纳答案by shellter

less will look in its environment to see if there is a variable named LESS

less 会在它的环境中查看是否有一个名为 LESS 的变量

You can set LESS in one of your ~/.profile (.bash_rc, etc, etc) and then anytime you run lessfrom the comand line, it will find the LESS.

您可以在您的 ~/.profile (.bash_rc 等)之一中设置 LESS,然后无论何时从命令行运行less,它都会找到 LESS。

Try adding this

尝试添加这个

export LESS="-CQaix4"

This is the setup I use, there are some behaviors embedded in that may confuse you, so you can find out about what all of these mean from the help function in less, just tap the 'h' key and nose around, or run less --help.

这是我使用的设置,其中嵌入的一些行为可能会让您感到困惑,因此您可以从 中的帮助功能中了解所有这些的含义less,只需轻按“h”键和鼻子,或运行less --help

Edit:

编辑:

I looked at the help, and noticed there is also an -roption

我查看了帮助,发现还有一个-r选项

-r  -R  ....  --raw-control-chars  --RAW-CONTROL-CHARS
                Output "raw" control characters.

I agree that catmay be the most exact match to your stated needs.

我同意这cat可能与您陈述的需求最匹配。

cat -vet file | less

Will add '$' at end of each line and convert tab char to visual '^I'.

将在每行末尾添加 '$' 并将制表符转换为可视的 '^I'。

cat --help
   (edited)
    -e                       equivalent to -vE
    -E, --show-ends          display $ at end of each line
    -t                       equivalent to -vT
    -T, --show-tabs          display TAB characters as ^I
    -v, --show-nonprinting   use ^ and M- notation, except for LFD and TAB

I hope this helps.

我希望这有帮助。

回答by Costi Ciudatu

You can do that with cat and that pipe the output to less:

你可以用 cat 做到这一点,并将输出管道减少:

cat -e yourFile | less

This excerpt from man catexplains what -emeans:

这段摘录man cat解释了什么-e意思:

   -e     equivalent to -vE

   -E, --show-ends
          display $ at end of each line

   -v, --show-nonprinting
          use ^ and M- notation, except for LFD and TAB

回答by thiton

All special, nonprintable characters are displayed using ^ notation in less. However, line feed is actually printable (just make a new line), so not considered special, so you'll have problems replacing it. If you just want to see line endings, the easiest way might be

所有特殊的、不可打印的字符都使用 ^ 表示法显示。但是,换行符实际上是可打印的(只需创建一个新行),因此不被认为是特殊的,因此替换它时会遇到问题。如果您只想查看行尾,最简单的方法可能是

sed -e 's/$/$/' | less

回答by James Brown

For lessuse -uto display carriage returns (^M) and backspaces (^H), or -Uto show the previous and tabs (^I) for example:

对于less使用-u于显示回车(^M)和退格(^H),或-U以显示先前的和接片(^I)例如:

$ awk 'BEGIN{print "foo\bbar\tbaz\r\n"}' | less -U 
foo^Hbar^Ibaz^M

(END)

Without the -Uswitch the output would be:

如果没有-U开关,输出将是:

fobar   baz

(END)

See man lessfor more exact description on the features.

有关man less功能的更准确描述,请参阅。

回答by flow2k

In the same spirit as https://stackoverflow.com/a/6943976/7154924:

本着与https://stackoverflow.com/a/6943976/7154924相同的精神:

cat -A

cat -A

-A, --show-all
       equivalent to -vET
-v, --show-nonprinting
       use ^ and M- notation, except for LFD and TAB
-E, --show-ends
       display $ at end of each line
-T, --show-tabs
       display TAB characters as ^I

Alternatively, or at the same time, you can pipe to trto substitute arbitrary characters to the desired ones for display, before piping to a pager like lessif desired.

或者,或者同时,您可以通过管道将tr任意字符替换为所需的显示字符,然后再通过管道传输到寻呼机(如less需要)。

回答by caxcaxcoatl

Now, sometimes you already have lessopen, and you can't use caton it. For example, you did a | less, and you can't just reopen a file, as that's actually a stream.

现在,有时你已经less打开了,你不能使用cat它。例如,你做了一个| less,你不能只是重新打开一个文件,因为它实际上是一个流。

If all you need is to identify end of line, one easy way is to search for the last character on the line: /.$. The search will highlight the last character, even if it is a blank, making it easy to identify it.

如果您只需要识别行尾,一种简单的方法是搜索行中的最后一个字符:/.$. 搜索将突出显示最后一个字符,即使它是一个空白字符,以便于识别。

That will only help with the end of line case. If you need other special characters, you can use the cat -vetsolution above with marks and pipe:

这只会对行尾情况有所帮助。如果您需要其他特殊字符,您可以使用cat -vet上面带有标记和管道的解决方案:

  • mark the top of the text you're interested in: ma
  • go to the bottom of the text you're interested in and mark it, as well: mb
  • go back to the mark a: 'a
  • pipe from ato bthrough cat -vetand view the result in another less command: |bcat -vet | less
  • 标记您感兴趣的文本的顶部: ma
  • 转到您感兴趣的文本的底部并标记它: mb
  • 回到标记a'a
  • 从管b通过cat -vet并查看结果在另一个较少的命令:|bcat -vet | less

This will open another lessprocess, which shows the result of running cat -veton the text that lies between marks aand b.

这将打开另一个less进程,它显示cat -vet在标记ab之间的文本上运行的结果。

If you want the whole thing, instead, do g|$cat -vet | less, to go to the first line and filter all lines through cat.

如果您想要整个事情,请执行g|$cat -vet | less, 转到第一行并通过 过滤所有行cat

The advantage of this method over lessoptions is that it does not mess with the output you see on the screen.

less选项相比,此方法的优势在于它不会干扰您在屏幕上看到的输出。

One would think that eight years after this question was originally posted, lesswould have that feature... But I can't even see a feature request for it on https://github.com/gwsw/less/issues

有人会认为在这个问题最初发布八年后,less会有那个功能......但我什至看不到https://github.com/gwsw/less/issues上的功能请求