问题描述
我有一个文件,我想在其中对以 -rwx 或 drwx 开头并以任意数字结尾的行进行 grep.
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]$' "$@"
参见:正则表达式和grep 了解 POSIX 标准的详细信息.
See: Regular Expressions and grep for POSIX-standard details.
这篇关于用于行首和行尾的 Grep?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!