Monday, April 29, 2024
 Popular · Latest · Hot · Upcoming
54
rated 0 times [  54] [ 0]  / answers: 1 / hits: 20147  / 2 Years ago, mon, february 28, 2022, 6:55:54

This



ls -l /var/log | awk '{print $9}' | grep "^[a-z]*.log."


outputs this:



alternatives.log.1
alternatives.log.10.gz
alternatives.log.2.gz
alternatives.log.3.gz
alternatives.log.4.gz
alternatives.log.5.gz
alternatives.log.6.gz
alternatives.log.7.gz
alternatives.log.8.gz
alternatives.log.9.gz
apport.log.1
apport.log.2.gz
apport.log.3.gz


but this:



ls -l /var/log | awk '{print $9}' | grep "^[a-z]+.log."


outputs nothing.



Why? I just changed * to +. Isn't it similar? Operator + just needs at least one match, and * zero or more.


More From » grep

 Answers
7

This is because grep (without any arguments) only works with standard regular expressions. + is part of the extended regular expressions, so to use that, you need to use grep -E or egrep:



ls -l /var/log | awk '{print $9}' | grep -E "^[a-z]+.log."


Also, you can just do this if you don't want to use extended regular expressions:



ls -l /var/log | awk '{print $9}' | grep "^[a-z][a-z]*.log."

[#31293] Wednesday, March 2, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ntlesslving

Total Points: 123
Total Questions: 109
Total Answers: 113

Location: South Korea
Member since Fri, Sep 11, 2020
4 Years ago
;