OGeek|极客世界-中国程序员成长平台

标题: Grep for beginning and end of line? [打印本页]

作者: 菜鸟教程小白    时间: 2022-5-24 13:56
标题: Grep for beginning and end of line?

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


Best Answer-推荐答案


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.






欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://www.ogeek.cn/) Powered by Discuz! X3.4