在 python 中使用 grep 命令

Use a grep command in python

有如下命令:

for i in $(objdump -d binary |grep "^ " |cut -f2); do echo -n '\x'$i; done;echo

如何在我的 python 代码中使用此命令,然后将 hexdump(打印)的结果传送到控制台。

    objdump = "$(objdump -d " + str(_arg_name) + "| grep '^ ' |cut -f2);" + " do echo -n $i; done; echo"
    result = os.popen(objdump).read()
    result = result.replace('"','')
    print(result)

您可以使用以下代码:

    g1 = "grep '^ '"
    g2 = "cut -f2"

    objdump = "objdump -d " + str(binary) + "|" + g1 + "|" + g2 
    result = os.popen(objdump).read()
    result = result.replace('\n','')
    result = result.replace(' ','')
    length = len(result) - 1
    result = [r'\x' + result[i:i + 2] for i in range(0, length, 2)]
    print(result)