On trying to run the grep for the output of previous command using popen returning blank without any error
JavaScript
x
10
10
1
proc1cmd = "grep " + fetchname1
2
p1 = subprocess.Popen(['kubectl', 'get', 'abr', '-A'], stdout=subprocess.PIPE)
3
p2 = subprocess.Popen(proc1cmd, shell=True, stdin=p1.stdout, stdout=subprocess.PIPE,
4
stderr=subprocess.PIPE)
5
p1.stdout.close()
6
stdout_list = p2.communicate()[0]
7
stdout_list = stdout_list.decode()
8
print(p2.stdout)
9
print(p2.communicate())
10
output i got:
JavaScript
1
3
1
<_io.BufferedReader name=7>
2
(b'', b'')
3
Advertisement
Answer
You don’t need to concoct a pipeline of kubectl + grep here.
JavaScript
1
8
1
kubectl_output = subprocess.check_output(
2
["kubectl", "get", "abr", "-A"], encoding="utf-8"
3
)
4
matching_lines = [
5
line for line in kubectl_output.splitlines() if fetchname1 in line
6
]
7
print(matching_lines)
8