grep命令开始和行结束? [英] Grep for beginning and end of line?

查看:569
本文介绍了grep命令开始和行结束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我想对grep与任何-rwx或drwx和结束的任何数字开始行的文件。

我已经得到了这一点,但它不是完全正确。任何想法?

 的grep [^ .rwx] * [0-9] usrLog.txt


解决方案

最棘手的部分是一个正则表达式,其中包括一个破折号作为字符类的有效字符之一。破折号必须开始一个(正常)字符类和插入符,对于一个否定的字符类后,立即后,立即来了。如果你需要一个紧密括号太多,那么你需要密切括号后面的冲刺。幸运的是,你只需要冲刺,因此符号选择。

 的grep'^ [ -  J]。RWX * [0-9] $$ @

请参阅:普通防爆pressions 并的grep 对POSIX标准的细节。

I have a file where I want to grep for lines that start with either -rwx or drwx AND end in any number.

I've got this, but it isnt quite right. Any ideas?

grep [^.rwx]*[0-9] usrLog.txt

解决方案

The tricky part is a regex that includes a dash as one of the valid characters in a character class. The dash has to come immediately after the start for a (normal) character class and immediately after the caret for a negated character class. If you need a close square bracket too, then you need the close square bracket followed by the dash. Mercifully, you only need dash, hence the notation chosen.

grep '^[-d]rwx.*[0-9]$' "$@"

See: Regular Expressions and grep for POSIX-standard details.

这篇关于grep命令开始和行结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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