What is the python equivalent of grep -v?
A regex in Python, either the search
or match
methods, returns a Match
object or None
. For grep -v
equivalent, you might use:
import re
for line in sys.stdin:
if re.search(r'[a-z]', line) is None:
sys.stdout.write(line)
Or more concisely:
import re; sys.stdout.writelines([line for line in sys.stdin if re.search(r'[a-z]', line) is None])