Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #106770

Re: Conditionals in Python cli with -m oneliner

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: Conditionals in Python cli with -m oneliner
Date Sun, 10 Apr 2016 00:15:53 +0200
Organization None
Lines 38
Message-ID <mailman.147.1460240164.2253.python-list@python.org> (permalink)
References <57094F5F.2080709@gmail.com> <nebuuq$rtn$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Trace news.uni-berlin.de 1+nPVPYE4uVZvFGg0E4YcACER3z/Xu5v8wFa4A9FB2KA==
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'url:pypi': 0.03; 'subject:Python': 0.05; 'python3': 0.05; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'text"': 0.09; 'python': 0.10; 'syntax': 0.13; '"some': 0.16; '--help': 0.16; '...]': 0.16; 'expr': 0.16; 'line)': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.16; '(see': 0.20; 'txt': 0.22; 'import': 0.24; 'header:User-Agent:1': 0.26; 'command': 0.26; 'header:X-Complaints-To:1': 0.26; 'error': 0.27; 'random': 0.29; 'statement': 0.32; 'url:python': 0.33; 'execution': 0.35; 'url:org': 0.36; 'lines': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'seem': 0.37; 'why': 0.39; 'does': 0.39; 'to:addr:python.org': 0.40; 'subject:with': 0.40; 'received:de': 0.40; 'talking': 0.67; '.....': 0.76; "'if": 0.84
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host p57bd95e2.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <nebuuq$rtn$1@ger.gmane.org>
X-Mailman-Original-References <57094F5F.2080709@gmail.com>
Xref csiph.com comp.lang.python:106770

Show key headers only | View raw


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


Thread

Re: Conditionals in Python cli with -m oneliner Peter Otten <__peter__@web.de> - 2016-04-10 00:15 +0200

csiph-web