Q:

How to perform grep onto the output of ldd properly ?

# 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...

A:

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.

A:

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)

A:

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.-]*"

