Grepping strace output gets hard

greppipestrace

I wrote the following code to determine which files a program writes to. I want to capture the filenames of course.

strace -f -t -e trace=file -p 8804 2>&1 | grep -oP "\"(.*)\".*O_WRONLY"

This outputs something like

/tmp/11111111.txt", O_WRONLY

The problem is I can't pipe the output of all this to any command

strace -f -t -e trace=file -p 8804 2>&1 | grep -oP "\"(.*)\".*O_WRONLY" | echo
# does not show anything

And also I can't save the output of all this for later use:

strace -f -t -e trace=file -p 8804 2>&1 | grep -oP "\"(.*)\".*O_WRONLY" > asd.out
# file is empty

Your help is appreciated. 🙂

Best Answer

You can write the output to a file (with strace -o asd.out) and then grep it:

From strace manual:

-o filename Write  the  trace  output to the file filename rather than 
to stderr.  Use filename.pid if -ff is used. If the argument begins with
`|' or with `!' then the rest of the argument is treated as a command
and all output is piped to it. This is convenient for piping the
debugging output to a program without affecting the redirections of 
executed programs.
Related Question