-   Linux - Software (
-   -   sed problems. possibly delimiter related (

luw 06-04-2013 01:29 PM

sed problems. possibly delimiter related
I am creating a script that pulls out certain path+files that get dumped from ldd. so as an example, say the program is named "cheezewiz".


$ ldd cheezewiz => /usr/lib/x86_64-linux-gnu/ (0x00007fcca6c37000) => /usr/lib/x86_64-linux-gnu/ (0x00007fcca69ba000) => /usr/lib/x86_64-linux-gnu/ (0x00007fcca6777000) => /usr/lib/x86_64-linux-gnu/ (0x00007fcca6537000) => /usr/lib/x86_64-linux-gnu/ (0x00007fcca6065000)

What is to be pulled out is the path and file from each line, and erase the rest. (1st line example "/usr/lib/x86_64-linux-gnu/")

I'm guessing a proper way to do this would use the "=> " as the first search point and the " (0" as the end. However I'm not sure how to extract this, nor what option would be best in sed. The main problem for me is I'm not sure if the => are special chars that need a "\" or what to do because the things that are searched for contain "/".

Does anyone have any tips or suggestions for how to approach this? I'd really apprecheate any response.

David the H. 06-04-2013 01:44 PM

There are lots of ways to kill this bird. The easiest is probably awk.


ldd cheezewiz | awk '/=>/{print $3}'
But if you really want to use sed, here are a couple of versions that should work too]


ldd cheezewiz | sed -rn '/=>/ s/.*=> ([^ ]+) .*/\1/p'
ldd cheezewiz | sed -rn '/=>/ s/(^.*=> | [(].*$)//gp'

By the way, sed's substitution delimiter can be any ascii character, not just '/'. It will use whatever comes directly after the 's', so just use something that doesn't appear in the expression itself. And no, '=' and '>' are not regex special.

Here are a few useful sed references:

Here are a few useful awk references:

Here are a few regular expressions tutorials:

H_TeXMeX_H 06-04-2013 02:32 PM

This is the most resilient method I've found:


ldd calcoo | sed 's/.*=> //' | awk '{ print $1 }'
It passes things like:

Code: (0x00007fff283bf000) => /usr/lib64/ (0x00007f7b1e140000) => /usr/lib64/ (0x00007f7b1de8e000) => /lib64/ (0x00007f7b184ef000)
        /lib64/ (0x00007f7b1e78e000) => /usr/lib64/ (0x00007f7b182d8000)

David the H. 06-04-2013 04:39 PM

Actually, I'd say the second sed command I posted is probably the most reliable. It simply removes everything before and after the desired string, matching them with clearly defined regexes. And all in a single process.

If it's a bit difficult to comprehend, it can also be written this way, with two separate substitutions:

ldd cheezewiz | sed -n '/=>/ { s/^.*=> // ; s/ [(].*$// ; p }'

luw 06-08-2013 02:01 AM

Thank you David the H. and H_TeXMeX_H, both work great. As for the links, it seems everytime I need to use sed for something like this it ends up getting figured out, however the very next time I need to use it again, little or nothing from the past can help with a new problem.

I'm checking out the sourceforge grabbag now. Best regards and thanks again for your help!

David the H. 06-09-2013 06:01 AM

Yeah, it does take time and effort to gain experience with the tools you use. I know that I had some hair-pulling times myself (still do, occasionally). Just keep plugging away, and it'll eventually get easier.

All times are GMT -5. The time now is 10:20 PM.