Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #106770
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Conditionals in Python cli with -m oneliner |
| Date | 2016-04-10 00:15 +0200 |
| Organization | None |
| Message-ID | <mailman.147.1460240164.2253.python-list@python.org> (permalink) |
| References | <57094F5F.2080709@gmail.com> <nebuuq$rtn$1@ger.gmane.org> |
gvim wrote:
> Given that this work in a Python 3 repl:
>
>>>> import re
>>>> txt = "Some random text"
>>>> if re.search(r"\b\w{4}\b", txt): txt
>>>> 'Some random text'
>
> .... and this works on the command line, printing all lines in logs.txt:
>
> $ python3 -m oneliner -ne 'line' logs.txt
>
> ..... why does this fail:
>
> $ python3 -m oneliner -m re -ne 'if re.search(r"\b\w{4}\b", line): line'
> logs.txt
> syntax error in: if re.search(r"\b\w{4}\b", line): line
You seem o be talking about https://pypi.python.org/pypi/oneliner
$ python3 -m oneliner --help
Usage: python -m oneliner [-hvnpdijl] [-m mod] [-e expr] [-s stmt] <path> [<path> ...]
[snip]
-e expr python expression (see Execution Model)
-s stmt python statement (see Execution Model)
So you can either use -e with an expression
$ python3 -m oneliner -m re -ne 'line if re.search(r"\b\w{4}\b", line) else ""'
or -s with a statement
$ python3 -m oneliner -m re -ns 'if re.search(r"\b\w{4}\b", line): print(line, end="")'
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Conditionals in Python cli with -m oneliner Peter Otten <__peter__@web.de> - 2016-04-10 00:15 +0200
csiph-web