bash grep'ing 后保留 ls 着色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/867877/
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
Preserve ls colouring after grep'ing
提问by duckyflip
If I do
如果我做
$ ls -l --color=always
I get a list of files inside the directory with some nice colouring for different file types etc..
我得到了目录中的文件列表,其中包含针对不同文件类型等的一些漂亮的着色。
Now, I want to be able to pipe the coloured output of lsthrough grepto filter out some files I don't need. The key is that I still want to preserve the colouring after the grep filter.
现在,我希望能管的彩色输出ls,通过grep过滤掉我并不需要一些文件。关键是我还是想保留grep过滤后的颜色。
$ ls -l --color=always | grep -E some_regex
^ I lose the colouring after grep
^ 我在 grep 之后失去了着色
EDIT: I'm using headless-server Ubuntu 8.10, Bash 3.2.39, pretty much a stock install with no fancy configs
编辑:我使用的是无头服务器 Ubuntu 8.10、Bash 3.2.39,几乎没有花哨的配置的股票安装
回答by lhunath
Your grep is probably removing ls's color codes because it has its own coloring turned on.
您的 grep 可能正在删除ls的颜色代码,因为它打开了自己的颜色。
You "could" do this:
你“可以”这样做:
ls -l --color=always | grep --color=never pattern
However, it is very important that you understand what exactly you're grepping here. Not only is grepping lsunnecessary (use a globinstead), this particular case is grepping through not only filenames and file stats, but also through the color codes added by ls!
但是,了解您在grep此处 ping 的确切内容非常重要。不仅grepping是ls不必要的(使用 aglob代替),这种特殊情况grep不仅可以通过文件名和文件统计信息来ping,还可以通过ls!添加的颜色代码来ping 。
The real answer to your question is: Don't grepit. There is never a need to pipe lsinto anything or capture its output. lsis only intended for human interpretation (eg. to lookat in an interactiveshell only, and for this purpose it is extremely handy, of course). As mentioned before, you can filter what files lsenumerates by using globs:
你的问题的真正答案是:不要grep。永远不需要管道ls进入任何东西或捕获其输出。 ls仅用于人类的解释(如要看看在一个互动的唯一外壳,并为此目的是非常方便的,当然)。如前所述,您可以ls使用 globs过滤枚举的文件:
ls -l *.txt # Show all files with filenames ending with `.txt'.
ls -l !(foo).txt # Show all files with filenames that end on `.txt' but aren't `foo.txt'. (This requires `shopt -s extglob` to be on, you can put it in ~/.bashrc)
I highlyrecommend you read these two excellent documents on the matter:
我强烈建议你阅读这两个关于此事的优秀文件:
- Explanation of the badness of parsing
ls: http://mywiki.wooledge.org/ParsingLs - The power of
globs: http://mywiki.wooledge.org/glob
- 解析不好的解释
ls:http://mywiki.wooledge.org/ParsingLs globs的力量:http://mywiki.wooledge.org/glob
回答by TheBonsai
You should check if you are really using the "real" ls, just by directly calling the binary:
你应该检查你是否真的在使用“真正的”ls,只需直接调用二进制文件:
/bin/ls ....
Because: The code you described really should work, unless ls ignores --color=always for some weird reason or bug.
因为:您描述的代码确实应该有效,除非 ls 出于某种奇怪的原因或错误而忽略 --color=always 。
I suspect some alias or function that adds (directly or through a variable) some options. Double-check that this isn't the case.
我怀疑某些别名或函数会(直接或通过变量)添加一些选项。仔细检查是否不是这种情况。

