Python oneliners with python -c '...'
are extremely restricted, as the Python grammar assumes that each statement sits on its own line. You can combine some statements with a semicolon “;
”, but some statements need to have their own line, notably compound statements like loops. If you want to write this on the command line, we have to express the loop over all lines as a list comprehension:
python3 -c 'import re, fileinput; [print(line, end="") for line in fileinput.input() if re.search("180205", line)]'
This is of course fairly unreadable, because Python is not well-suited for one-liners. Looping over the fileinput.input()
is similar to Perl's -n
option.
If you want to use Python, consider writing a script. This is much more readable:
import re, fileinput
for line in fileinput.input():
if re.search("180205", line):
print(line, end="")