Open bobpaul opened 7 years ago
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:
# pseudocode
for line in stdin:
if "bash" in line:
print(line.split(' ')[4])
elif "daemon" in line:
print(line.split(' ')[1])
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]" 792 839 846 1003
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:
$ cat ps.txt | python2 pyp3 "keep('daemon') or keep('bash') | p.split()[1] if 'daemon' in p else p.split()[4] if 'bash' in p else ''"
792
839
846
1003
21516
1339
21568
21480
2635
21516
$ cat ps | awk '/daemon/ { print $2 } /bash/ { print $5 }'
792
839
846
1003
21516
1339
21568
21480
2635
12944
21516
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?