-
Notifications
You must be signed in to change notification settings - Fork 13
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
Selecting columns for a list breaks multiple matches #2
Comments
My pyp is a little rusty - I don't get to use it as much as I'd like. I know how I'd do it with vanilla python:
I'll have to spend a bit playing with pyp again to see how it would work. |
I get a little bit closer if I only keep lines containing the strings you want. Now I get blanks for the second re. $cat pyp_test.txt | ./pyp3 "'bash' in p or 'daemon' in p" | ./pyp3 "p.re('.daemon.').split()[1] or p.re('.bash.').split()[4]" 1339 2635 I still kind of think this can be done as-is. Just need to think it through a bit more. |
Oh, you gave me an idea and I got very close:
The difference is there's one line that contains both bash and daemon. Awk is performing 2 independent IFs, whereas with the pyp statement above it's if-else. |
Maybe this can already be done and I'm just not getting it, but here's a contrived example to illulstrate.
Let's say I have some output of
ps aux
which looks like this:Now, for all lines that contain bash I want to print the 5th column. For all lines that contain daemon I want to print the 2nd column. This can be done in awk like:
So I try it to incrementally build the command with pyp... I start by matching both conditions, which after a bit of messing around, I figured out I could do with 'or'. (Maybe this is already abusive.)
Good so far. And grab the columns (remember awk is 1 indexed, python is 0):
Wait, that's not enough results. It's only shows the columns for daemon matches. I think what's happening is the
[1]
selector must cause the first part to evaluate to True in cases where the regex didn't match (returned None). (None[1] would cause an exception, so part of the exception handling routine must make it always return True).This becomes apparent if we remove the column selector from the daemon regex:
Now it's returning both matches again, but only selecting columns on the second match.
Am I just approaching this problem the wrong way, or is it not currently possible to replicate the awk code that outputs a different column depending on what within the line matched?
The text was updated successfully, but these errors were encountered: