preserve LS grep'ing着色后 [英] Preserve ls colouring after grep'ing

查看:90
本文介绍了preserve LS grep'ing着色后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我

$ ls -l --color=always

我得到的目录中的文件有一些不错的色彩不同文件类型列表等。

I get a list of files inside the directory with some nice colouring for different file types etc..

现在,我希望能够管 LS的彩色输出的grep 来过滤掉一些文件我不需要。关键是,我还是想preserve grep的过滤器后着色。

Now, I want to be able to pipe the coloured output of ls through grep to filter out some files I don't need. The key is that I still want to preserve the colouring after the grep filter.

$ ls -l --color=always | grep -E some_regex

^我失去的grep后着色

^ I lose the colouring after grep

编辑:我使用的是无头服务器的Ubuntu 8.10,猛砸3.2.39,pretty太大的股票安装带有没有花哨的CONFIGS

I'm using headless-server Ubuntu 8.10, Bash 3.2.39, pretty much a stock install with no fancy configs

推荐答案

您的grep可能是删除 LS的颜色codeS,因为它有自己的色彩转身上。

Your grep is probably removing ls's color codes because it has its own coloring turned on.

您可能做到这一点:

ls -l --color=always | grep --color=never pattern

不过,你明白究竟你是的grep 平在这里是非常重要的。不仅是的grep LS 不必要的(使用水珠代替),这种特殊的情况下,为的grep ping通不仅是文件名和文件的统计信息,还可以通过颜色codeS通过 LS添加

However, it is very important that you understand what exactly you're grepping here. Not only is grepping ls unnecessary (use a glob instead), this particular case is grepping through not only filenames and file stats, but also through the color codes added by ls!

真正的回答你的问题是:不要的grep 它。从未有一个需要管 LS 成任何东西,或捕捉它的输出。 LS 仅用于人与人之间pretation(如到的的在一个的交互的外壳而已,并为此目的是非常方便的,当然)。正如前面提到的,你可以过滤哪些文件 LS 列举使用水珠:

The real answer to your question is: Don't grep it. There is never a need to pipe ls into anything or capture its output. ls is only intended for human interpretation (eg. to look at in an interactive shell only, and for this purpose it is extremely handy, of course). As mentioned before, you can filter what files ls enumerates by using 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 highly recommend you read these two excellent documents on the matter:

这篇关于preserve LS grep'ing着色后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆