Off topic: When I was on Linux, I saw last | grep [a-zA-Z] | grep -v 'wtmp' | wc -l. I was wondering if I could do it with just one grep, so I thought about it. Use regular expression to match a certain line that does not contain a certain word (such as wtmp), but the result is not figured out.
淡淡烟草味2017-05-24 11:34:15
js/^(?!.*wtmp)/
> p1=/^(?!.*wtmp)/
/^(?!.*wtmp)/
> p1.test('abc123')
true
> p1.test('abc1wtmp23')
false
>
为情所困2017-05-24 11:34:15
Matching does not contain, this difficulty should be relatively large for regular expressions, because this condition may be true at this position, but it may not be true at the next position! However, regular expressions will return any string that satisfies pattern
For As an example of your question, I recommend using awk
to implement:
last | awk '!=wtpm&&/[A-Za-z]/{num++}END{print num}'
awk
It can provide more flexible logical processing and is more readable. Compared with a single regular expression, I think awk is a better choice in the scenario in the question, but if it is implemented in other languages, you may have to refer to In other languages