3
# ldd /usr/bin/ffmpeg
    linux-vdso.so.1 =>  (0x00007ffffc1fe000)
    libavfilter.so.0 => not found
    libpostproc.so.51 => not found
    libswscale.so.0 => not found
    libavdevice.so.52 => not found
    libavformat.so.52 => not found
    libavcodec.so.52 => not found
    libavutil.so.49 => not found
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fdd18259000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fdd1803a000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fdd17c75000)
    /lib64/ld-linux-x86-64.so.2 (0x00007fdd18583000)

I am trying to grep only the names left from the "=>" symbol.

It works with echo easily:

echo linux-vdso.so.1 | grep -oP "^[a-z0-9.]*"
linux-vdso.so.1

But when I perform the same RegEx onto the output of ldd it does display anything:

ldd /usr/bin/ffmpeg | grep -oP "^[a-z0-9.]*"

So I thought, maybe I have to include some whitespace

ldd /usr/bin/ffmpeg | grep -oP "^([a-z0-9.]|\w)*"

But, this did not work and so I do not know further...

4 Answers4

5

The best solution for this is to use awk:

$ ldd /usr/bin/ppdhtml | awk '/ => / { print $1 }' | head -n1
libcupsppdc.so.1

To do this using grep, you will need to use the lookahead and lookbehind features of PCRE:

$ ldd /usr/bin/ppdhtml | grep -Po '(?<=\t).+(?= => )' | head -n1
libcupsppdc.so.1

The lookahead and lookbehind features affect that match, but are not included in the match. Also note that this would not work if ldd used a variable number of spaces instead of tabs at the start of the line. Lookbehinds can not have a variable length.

jordanm
  • 42,678
  • perfect, lookbehind and lookahead are good friends on both sides, thanks for teaching a nitwit some trick. – Abdul Al Hazred Apr 02 '15 at 17:37
  • @jordanm: I don't think lookahead and lookbehind make sense here, since when we can easily match with simple regex. – cuonglm Apr 02 '15 at 17:41
  • @cuonglm the simple regex from your example leaves the leading tabs and produces a line that only contains a tab. – jordanm Apr 02 '15 at 17:52
4

One possibility, use cut:

$ cat junk.txt

linux-vdso.so.1 => (0x00007ffffc1fe000)

libavfilter.so.0 => not found
libpostproc.so.51 => not found
libswscale.so.0 => not found
libavdevice.so.52 => not found
libavformat.so.52 => not found
libavcodec.so.52 => not found
libavutil.so.49 => not found
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fdd18259000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fdd1803a000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fdd17c75000)
/lib64/ld-linux-x86-64.so.2 (0x00007fdd18583000)

$ cat junk.txt | cut -d'=' -f1

libavfilter.so.0

libpostproc.so.51 

libswscale.so.0 

libavdevice.so.52 

libavformat.so.52 

libavcodec.so.52 

libavutil.so.49 

libm.so.6 

libpthread.so.0 

libc.so.6 

/lib64/ld-linux-x86-64.so.2 (0x00007fdd18583000)
Baazigar
  • 730
  • If you want to exclude exclude /lib64/ld-linux-x86-64.so.2: ldd /usr/bin/ffmpeg | grep '=' | cut -d'=' -f1 – Baazigar Apr 02 '15 at 18:34
3

You have to use \s instead of \w (which match a word character) to match any single character considered whitespace, include [\t\n\f\r ]:

ldd /usr/bin/ffmpeg | grep -oP "^([a-z0-9.-]|\s)*"

or:

ldd /usr/bin/ffmpeg | grep -oP "^\s*[a-z0-9.-]*"
cuonglm
  • 153,898
1

This handles shared objects that have white spaces in the file names

ldd /usr/bin/ffmpeg | grep -Po '(?i).+ => \K.+(?= \([A-F0-9]x[A-F0-9]+\))'
hb2638
  • 11