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
Solution 1:
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.
Solution 2:
It looks like you were on the right track... The ^
character matches beginning-of-line, and $
matches end-of-line. Jonathan's pattern will work for you... just wanted to give you the explanation behind it