The options for nm
are not very consistent across machines, and neither are the output formats. However, there is usually an option that will prefix the file name (object file or archive member or executable name) to the output. On MacOS X, that option is -o
; on Solaris, it is -r
or -R
; on Linux, it is -o
, ...
nm -g -o $(<executables.out) | grep -w symbol | awk -F: '{print $1}'
This will list files the define or reference the given symbol. To show only files that define a symbol, then you need to look to 'T' (functions) or 'C' (uninitialized or common data) or 'D' (initialized data) — though System V systems use different systems again:
nm -g -o $(<executables.out) | grep -w symbol | grep ' [TCD] ' | awk -F: '{print $1}'
The $(<file)
notation reads the named file and uses the contents as a series of arguments, but does so without actually executing a command (unlike the notation with back-ticks that executed awk
in the original questions; cat
would have been a reasonable alternative to awk
).
The grep -w
looks for the pattern as a whole word, so if you search for printf
, it won't print fprintf
, snprintf
, vsnprintf
, etc. It is a GNU grep
extension.