如何在Python subprocess.Popen中使用awk?

问题描述 投票:0回答:1

下面是我的Python代码,我需要使用awk来grep k8s pod内运行的进程(SWeNP)之一。

cs_swo_cmd = f"oc exec -it {pod_name} bash -c cs-container -- ps -eaf | grep -i 'SWe_NP' | awk \'{print $2}\'"
swo_process = subprocess.Popen(cs_swo_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

但是当我运行代码时,出现以下错误,请帮助。

stella@bts16:~$ python3 k8sSwitchover.py
  File "k8sSwitchover.py", line 70
    cs_swo_cmd = f"oc exec -it $i bash -c cs-container -- ps -eaf | grep -i 'SWe_NP' | awk {'print $2}"
                 ^
SyntaxError: f-string: unterminated string
stella@bts16:~$
python linux awk
1个回答
0
投票

尝试这个代替

cw_swo_cmd

cs_swo_cmd = f"oc exec -it {pod_name} bash -c cs-container -- ps -eaf | grep -i 'SWe_NP' | awk \'{{print $2}}\'"

应该给出正确的结果:

"oc exec -it $i bash -c cs-container -- ps -eaf | grep -i 'SWe_NP' | awk '{print $2}'"
© www.soinside.com 2019 - 2024. All rights reserved.