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

  • 3
    Why not awk '{ print $1 }'? – jordanm Apr 2 '15 at 17:21
  • works fine, I am just curious about this white-space thing, because the output of the command is strangely shiftet to the right, and I fought I might be able to catch it with the \w whitespace-class , but it does not work. I wanted to use the grepped names as input to apt-get in order to install all the missing ones automatically. – Abdul Al Hazred Apr 2 '15 at 17:32
  • 1
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.

|improve this answer|||||
  • perfect, lookbehind and lookahead are good friends on both sides, thanks for teaching a nitwit some trick. – Abdul Al Hazred Apr 2 '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 2 '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 2 '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)
|improve this answer|||||
  • If you want to exclude exclude /lib64/ld-linux-x86-64.so.2: ldd /usr/bin/ffmpeg | grep '=' | cut -d'=' -f1 – Baazigar Apr 2 '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.-]*"
|improve this answer|||||
  • The cut example is incomplete. He wants to exclude /lib64/ld-linux-x86-64.so.2 from the output. – jordanm Apr 2 '15 at 17:34
  • 1
    @jordanm: removed it. – cuonglm Apr 2 '15 at 17:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.