Skip to content

Instantly share code, notes, and snippets.

@ntlk
Last active August 1, 2021 18:59
Show Gist options
  • Save ntlk/7162075 to your computer and use it in GitHub Desktop.
Save ntlk/7162075 to your computer and use it in GitHub Desktop.
grep made bad in python
import sys
import re
pattern = re.compile(sys.argv[1])
last = int(len(sys.argv)) - 1
reverse = True if sys.argv[last] == '-v' else False
lines = sys.stdin.readlines()
for line in lines:
if bool(pattern.search(line)) != reverse:
sys.stdout.write(line)
@igalic
Copy link

igalic commented Oct 26, 2013

w..why?

(also, why not ack?)

@pkqk
Copy link

pkqk commented Oct 26, 2013

You can just do:
reverse = sys.argv[last] == '-v'
Instead of the if/else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment