1

I am trying to pipe a output from command to grep.

$ strace a.out | grep --color=always "mmap"

but it only outputs non-colorized output of 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

Here it is not colorized, but should. How to achieve it?

5
  • does echo 'foo mmap 123' | grep --color=always 'mmap' give you colored output? If so, strace output is probably on stderr and you are seeing entire output instead of just the lines with mmap – Sundeep Jun 22 '20 at 9:55
  • 1
    @Sundeep you right, I forgot, the strace gives a output to stderr, I thing there is a option in strace to redicrect its output, which could solve it – milanHrabos Jun 22 '20 at 9:59
  • @Sundeep I have tried this: strace a.out -o >(grep --color=always brk), but still no color – milanHrabos Jun 22 '20 at 10:01
  • try strace a.out 2>&1 | grep or strace a.out |& grep – Sundeep Jun 22 '20 at 10:05
  • @Sundeep thanks – milanHrabos Jun 22 '20 at 10:12
0

Since strace output is on the standard error stream, use

strace a.out 2>&1 | grep --color=always "mmap"

or, if you're using bash,

strace a.out |& grep --color=always "mmap"

See also Piping STDERR vs. STDOUT for more details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.