New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Weird error with $variable and grep in command line magic (!command) #3404
Comments
That regex looks like you'll end up matching names that start with the exact string |
I'm not trying to escape the braces. I'm trying to match all names with a given prefix. This was the use case in which I stumbled across the apparent error mentioned. |
To put it another way: if you're trying to pass grep the regex |
IPython applies Python string formatting to your subcommands, so In [2]: ip = get_ipython()
In [3]: ip.var_expand('!ls $dir')
Out[3]: u'!ls /Users/minrk/temp/somedir'
In [4]: ip.var_expand('!ls $dir | grep "^ab{2}"')
Out[5]: u'!ls /Users/minrk/temp/somedir | grep "^ab2"'
In [5]: ip.var_expand('!ls $dir | grep "^ab\{2\}"')
Out[5]: '!ls $dir | grep "^ab\\{2\\}"'
In [6]: ip.var_expand('!ls $dir | grep "^ab\{{2\}}"')
Out[6]: u'!ls /Users/minrk/temp/somedir | grep "^ab\\{2\\}"'
In [7]: !ls $dir | grep "^ab\{{2\}}"
abb-1
abb-2
abb-3
|
Ah, I think I get it now. In[1]: !\ls "/path/to/some/dir/" | grep "^ab\{2\}" works fine because variable expansion isn't triggered as there's no In[2]: adir = "/path/to/some/dir/"
...: !\ls $adir | grep "^ab\{2\}" breaks because the I can fix my notebook now. |
That's close, but a little bit backwards - it's the attempted expansion of In [1]: a = 5
In [2]: !echo a=$a {2}
a=5 2
In [3]: !echo a=$a {2\\}
a= {2\}
In [4]: !echo a=$a {{2}}
a=5 {2}
In [5]: !echo a=$a {{2\\}}
a=5 {2\} |
Ah, yes. So expansion/interpretation is always happening and it fails on the |
Yes, this is a key point, I should have mentioned that. |
With the latest stable version of IPython the following works fine in the IPython notebook (I had to unalias ls for my particular case, not sure if that's part of the error):
However, the following should provide the same output but instead provides the output shown:
In this second case the matching directories aren't listed. It seems to involve the use of
\{#\}
in grep as things work if I simply try to match"^a"
even with the use of$dir
instead of the literal directory string.The text was updated successfully, but these errors were encountered: