我正在尝试将命令的输出通过管道传输到 grep。
$ strace a.out | grep --color=always "mmap"
但它只输出 strace 的非彩色输出:
...
mmap(NULL, 503808, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa42389d000
write(1, "got memory: (address=0x7fa42389d"..., 37got memory: (address=0x7fa42389d010)
) = 37
mmap(NULL, 503808, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa423822000
write(1, "got memory: (address=0x7fa423822"..., 37got memory: (address=0x7fa423822010)
) = 37
mmap(NULL, 503808, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa4237a7000
write(1, "got memory: (address=0x7fa4237a7"..., 37got memory: (address=0x7fa4237a7010)
) = 37
mmap(NULL, 503808, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa42372c000
write(1, "got memory: (address=0x7fa42372c"..., 37got memory: (address=0x7fa42372c010)
) = 37
这里它没有着色,但应该着色。如何实现?
答案1
由于strace
输出位于标准错误流上,因此使用
strace a.out 2>&1 | grep --color=always -- 'mmap';
或者,如果您正在使用bash
,
strace a.out |& grep --color=always -- 'mmap';
如果需要所有可用输出:
strace a.out |& grep --color=always -E -- 'mmap|';
也可以看看管道 STDERR 与 STDOUT更多细节。