Strace output extract filenames from double quotes

open("/etc/ld.so.cache", O_RDONLY)      = 3
open("/lib/libselinux.so.1", O_RDONLY)  = 3
open("/lib/librt.so.1", O_RDONLY)       = 3
open("/lib/libacl.so.1", O_RDONLY)      = 3
open("/lib/libc.so.6", O_RDONLY)        = 3
open("/lib/libdl.so.2", O_RDONLY)       = 3
open("/lib/libpthread.so.0", O_RDONLY)  = 3
open("/lib/libattr.so.1", O_RDONLY)     = 3
......................

      

The above lines are the typical output of running strace with a file option.

I want to filter out filenames (with / without directory) in double quotes from the above output ... for example my requirement is to get the following list.

/etc/ld.so.cache
/lib/libselinux.so.1
 ..........

      

How to get filtered filenames using regex utility (grep / sed / awk / etc ..) on linux?

+3


source to share


3 answers


You can use this one grep

:

grep -oP '"\K[^\n"]+(?=")' file
/etc/ld.so.cache
/lib/libselinux.so.1
/lib/librt.so.1
/lib/libacl.so.1
/lib/libc.so.6
/lib/libdl.so.2
/lib/libpthread.so.0
/lib/libattr.so.1

      



This regex searches for a double quote and then discards the matching result with \K

. It finds the text between the double quotes, using [^\n"]+

followed by a lookahead (?=")

to make sure there is a matching double quote on the other side.

Demo version of RegEx

+2


source


Simple and convenient:

$ strace -f -e trace=file  true 2>&1 | awk -F'"' '{print $2}'
/usr/bin/true
/etc/ld.so.preload
/etc/ld.so.cache
/usr/lib/libc.so.6

      



As long as the output of the command strace

consists of files in double quotes, I tell awk (c -F

) to use it as a delimiter, then print the second column based on the split line for each line.

+1


source


You can use grep

for example.

grep ^open in.txt | grep -o '".\+[^"]"'

      

To filter out double quotes, add tr -d '"'

.

And here is the one-liner with strace

(change php

to your command name):

strace -fe trace=file -p $(pgrep -n php) 2>&1 | grep ^stat | grep -o '".\+[^"]"'

      

0


source







All Articles