LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   sed problems. possibly delimiter related (https://www.linuxquestions.org/questions/linux-software-2/sed-problems-possibly-delimiter-related-4175464700/)

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

Code:

$ ldd cheezewiz
        libQtGui.so.4 => /usr/lib/x86_64-linux-gnu/libQtGui.so.4 (0x00007fcca6c37000)
        libQtDBus.so.4 => /usr/lib/x86_64-linux-gnu/libQtDBus.so.4 (0x00007fcca69ba000)
        libQtXml.so.4 => /usr/lib/x86_64-linux-gnu/libQtXml.so.4 (0x00007fcca6777000)
        libQtSql.so.4 => /usr/lib/x86_64-linux-gnu/libQtSql.so.4 (0x00007fcca6537000)
        libQtCore.so.4 => /usr/lib/x86_64-linux-gnu/libQtCore.so.4 (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/libQtGui.so.4")

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.

Code:

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

Code:

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:
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt
http://www.catonmat.net/series/sed-one-liners-explained

=====
Here are a few useful awk references:
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html
http://www.pement.org/awk/awk1line.txt
http://www.catonmat.net/series/awk-one-liners-explained

=====
Here are a few regular expressions tutorials:
http://mywiki.wooledge.org/RegularExpression
http://www.grymoire.com/Unix/Regular.html
http://www.regular-expressions.info/

H_TeXMeX_H 06-04-2013 02:32 PM

This is the most resilient method I've found:

Code:

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

Code:

        linux-vdso.so.1 (0x00007fff283bf000)
        libgtk-x11-2.0.so.0 => /usr/lib64/libgtk-x11-2.0.so.0 (0x00007f7b1e140000)
        libgdk-x11-2.0.so.0 => /usr/lib64/libgdk-x11-2.0.so.0 (0x00007f7b1de8e000)
        librt.so.1 => /lib64/librt.so.1 (0x00007f7b184ef000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f7b1e78e000)
        libgcc_s.so.1 => /usr/lib64/libgcc_s.so.1 (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:
Code:

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.