2

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?

milanHrabos
  • 381
  • 2
  • 4
  • 12
  • 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 09: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 09: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

1 Answers1

1

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';

In case all the available output is required:

strace a.out |& grep --color=always -E -- 'mmap|';

See also Piping STDERR vs. STDOUT for more details.

Artfaith
  • 474
Sundeep
  • 12,008