Python -c switch

felix001 picture felix001 · Apr 5, 2013 · Viewed 9.7k times · Source

I currently have

".".join(str(z) for z in [int(x, 16) for x in (re.sub(r'(.{2})(?!$)', r'\1.', "00112233")).split('.')])
'xx.xx.xx.xx'

which works but when i try to use it via the python -c switch it fails ?

[root@monty ~]# python -c "import re ; ".".join(str(z) for z in [int(x, 16) for x in (re.sub(r'(.{2})(?!$)', r'\1.', "00112233")).split('.')])"
python -c "import re ; ".".join(str(z) for z in [int(x, 16) for x in (re.sub(r'(.{2})(?"import re ; ".".join(str(z) for z in [int(x, 16) for x in (re.sub(r'(.{2})(?python)', r'\1.', "00112233")).split('.')])")', r'\1.', "00112233")).split('.')])"
-bash: syntax error near unexpected token `str'

Any ideas ?

Answer

unwind picture unwind · Apr 5, 2013

Looks like a quoting issue on the command line.

Try wrapping the Python string in single quotes instead, and not using single quotes inside it.

You can also escape the quotes that collide with the shell's interpretation, using \".

$ python -c 'import re;print ".".join(str(z) for z in [int(x, 16) for x in (re.sub(r"(.{2})(?!$)", r"\1.", "00112233")).split(".")])'
0.17.34.51

Note: as your not running the python interpretor any more you need to explicitly print the results.