Visit Our UNIX and Linux User Community


Cannot extract libraries using sed


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Cannot extract libraries using sed
# 1  
Old 08-13-2019
Cannot extract libraries using sed

Hi,

I am attempting to extract the /lib/ paths using sed but it does not appear to work.

Code:
./copy_chroot_lib.sh ls echo | sed s#*\(/lib\).*#\1#g
        linux-vdso.so.1 (0x00007fff77df1000)
        libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f28190ac000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f2818ec2000)
        libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f2818e4e000)
        libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f2818e48000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f2819303000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f2818e27000)
        linux-vdso.so.1 (0x00007ffca8342000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f485dc28000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f485de28000)

 ./copy_chroot_lib.sh ls echo | sed -n s#\(/lib\).*#\1#pg

Desired output

Code:
/lib/x86_64-linux-gnu/libselinux.so.1
...
/lib64/ld-linux-x86-64.so.2

Can you please tell me what I am doing wrong?

Thanks.
# 2  
Old 08-13-2019
Please post the output of this command only:

Code:
./copy_chroot_lib.sh ls echo

# 3  
Old 08-13-2019
UPDATE: I have managed to get the desired output using tr/awk, but continue to leave it open for alternative solutions. Thanks!

Code:
./copy_chroot_lib.sh ls echo | tr '=>' '\n' | awk '/\/lib/{print $1}' | sort | uniq
/lib64/ld-linux-x86-64.so.2
/lib/x86_64-linux-gnu/libc.so.6
/lib/x86_64-linux-gnu/libdl.so.2
/lib/x86_64-linux-gnu/libpcre.so.3
/lib/x86_64-linux-gnu/libpthread.so.0
/lib/x86_64-linux-gnu/libselinux.so.1


--- Post updated at 04:43 AM ---

Hi Neo,

Sure.

Code:
 ./copy_chroot_lib.sh ls echo
        linux-vdso.so.1 (0x00007ffeba118000)
        libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f9b0d737000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f9b0d54d000)
        libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f9b0d4d9000)
        libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f9b0d4d3000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f9b0d98e000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f9b0d4b2000)
        linux-vdso.so.1 (0x00007ffd9c8cf000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f2f31a69000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f2f31c69000)

Thanks.
# 4  
Old 08-13-2019
The sed expression should be in quotes, so the shell does no substitutions on special characters.
Code:
sed -n 's#\(/lib\).*#\1#p'

Or
Code:
sed -n 's#.* => ##p'

This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 08-13-2019
Hi MadeinGermany,

Thanks for the tips.

The first solution does not work,

Code:
 ./copy_chroot_lib.sh ls echo | sed -n 's#\(/lib\).*#\1#p'
        libselinux.so.1 => /lib
        libc.so.6 => /lib
        libpcre.so.3 => /lib
        libdl.so.2 => /lib
        /lib
        libpthread.so.0 => /lib
        libc.so.6 => /lib
        /lib

While the output from the second solution does not capture only file path, and also captures (0x00...)

Code:
 ./copy_chroot_lib.sh ls echo | sed -n 's#.* => ##p'
/lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f73bfead000)
/lib/x86_64-linux-gnu/libc.so.6 (0x00007f73bfcc3000)
/lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f73bfc4f000)
/lib/x86_64-linux-gnu/libdl.so.2 (0x00007f73bfc49000)
/lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f73bfc28000)
/lib/x86_64-linux-gnu/libc.so.6 (0x00007f2cfa01c000)

I suppose we can exclude the hex value via the following, then capture lines starting with /lib.

Code:
 ./copy_chroot_lib.sh ls echo | sed -E 's#.* => ##;s/[(]0.*//'
        linux-vdso.so.1   <----- should not be included
/lib/x86_64-linux-gnu/libselinux.so.1
/lib/x86_64-linux-gnu/libc.so.6
/lib/x86_64-linux-gnu/libpcre.so.3
/lib/x86_64-linux-gnu/libdl.so.2
        /lib64/ld-linux-x86-64.so.2
/lib/x86_64-linux-gnu/libpthread.so.0
        linux-vdso.so.1
/lib/x86_64-linux-gnu/libc.so.6
        /lib64/ld-linux-x86-64.so.2

Do let me know if you have any better ideas.

Thanks!
# 6  
Old 08-13-2019
Sorry, in my first sample the .* (capture any characters) was wrongly at the end, belongs to the beginning.
You can add a second substitution, the trick is the order.
Code:
sed -n 's# *[(].*##; s#.* => ##p'

With awk
Code:
awk '$2 == "=>" {print $3}'

This User Gave Thanks to MadeInGermany For This Post:
# 7  
Old 08-13-2019
There are a few reasons why your original sed script can't work.
  1. Your first asterisk in your RE needs a period before it.
  2. There is nothing in your script to avoid printing lines that do not contain /lib.
  3. There is nothing in your RE that will stop looking when the first occurrence of /lib is found (and if you're looking for /lib instead of something like => you're going to have more problems because /lib occurs twice in most of the fields you want to print.
Since your sed command contains unquoted shell pathname matching meta characters there is also a slight danger that the RE in your sed substitute command could be destroyed by actually matching the pathname of an existing file.

Note that in post #3 where you said you had the output you wanted, there was a line in the output:
Code:
/lib64/ld-linux-x86-64.so.2

that does not appear in your subsequent posts nor in the output produced by MadeInGermany's suggestions. Note that there is no => in the input lines:
Code:
        /lib64/ld-linux-x86-64.so.2 (0x00007f2819303000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f485de28000)

which are the two input lines that end up producing the missing line of output.

If you still want that line of output try the command:
Code:
../copy_chroot_lib.sh ls echo |
	sed -n '/\/lib/s#.* \(/lib[^ ]*\) .*#\1#p' |
	sort -u

and see if it works for you. It works OK for me when using ksh or bash on macOS Mojave (version 10.14.6) with the sample data you provided. If the leading spaces shown in your sample data is really a tab character instead of being eight spaces, you will need to make a minor adjustment to the BRE. Note that invoking tr and uniq is not needed. The work that they do can all be done by sed and sort.

If you don't need the output sorted, but just want to get rid of duplicates, you can also try the following which just needs awk (with no sort required):
Code:
./copy_chroot_lib.sh ls echo |
	awk '	$(NF-1) ~ "^/lib" {	o[$(NF- 1)]		}
		END {			for(f in o) print f	}'

This User Gave Thanks to Don Cragun For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #282
Difficulty: Easy
Time-sharing operating systems schedule tasks for efficient use of the system and may also include accounting software for cost allocation of processor time, mass storage, printing, and other resources.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract a value using sed (easy)

I have this command to replace the version value from PROGRAM (for example here PROGRAM == player) by NEWVERSION sed "/^ *$PROGRAM:/{N; s/*$/ $NEWVERSION/;}" -i $PRDFILE player: version: V6R2013xD3HF5v1 player_old: version: V6R2013xD3HF5v1 partchecker: version:... (2 Replies)
Discussion started by: jcanale
2 Replies

2. Shell Programming and Scripting

sed to extract all strings

Hi, I have a text file containing 2 lines as follows: I'm trying to extract all the strings following an "AME." The output would be as follows: BUSINESS_UNIT PROJECT_ID ACTIVITY_ID RES_USER1 RESOURCE_ID_FROM ANALYSIS_TYPE BI_DISTRIB_STATUS BUSINESS_UNIT PROJECT_ID ACTIVITY_ID... (5 Replies)
Discussion started by: simpletech369
5 Replies

3. Shell Programming and Scripting

sed data extract

Hello, I have huge number files in a directory. All files have the data. I want extract data. I want all output write to single csv file. following codes works. Thank you very much for help. sed -n '/.*Content$txtE_Zip" type="text" value="\(*\)" maxlength.*/s//\1/p' *>file1 sed -n... (5 Replies)
Discussion started by: hoo
5 Replies

4. Shell Programming and Scripting

Extract using sed

Given a file with contents /arch/powerpc/boot/4xx.o > /arch/powerpc/boot/.4xx.o.cmd > /arch/powerpc/boot/addnote 5766a5769 > /arch/powerpc/boot/.addnote.cmd 5768a5772,5773 > /arch/powerpc/boot/bamboo.o > /arch/powerpc/boot/.bamboo.o.cmd 5769a5775,5778 > /arch/powerpc/boot/cpm-serial.o... (8 Replies)
Discussion started by: xerox
8 Replies

5. Shell Programming and Scripting

sed extract from xml

I have an xml file that generally looks like this: "<row><dnorpattern>02788920</dnorpattern><description/></row><row><dnorpattern>\+ 44146322XXXX</dnorpattern><description/></row><row><dnorpattern>40XXX</dnorpattern><description/></row><row><dnorpattern>11</dn... (4 Replies)
Discussion started by: garboon
4 Replies

6. Shell Programming and Scripting

Extract word using sed

Hello, I am new to sed and am trying to extract a word using sed. for example i have a line "const TotalAmount& getTotalAmount() const; " in the file test.txt I am trying to extract getTotalAmount() from the line. For this i tried cat test.txt | sed -n 's/.*get*\(\)//p But... (8 Replies)
Discussion started by: prasbala
8 Replies

7. Shell Programming and Scripting

SED extract XML value

I have the following string: <min-pool-size>2</min-pool-size> When I pipe the string into the following code I am expcting for it to return just the value "2", but its just reurning the whole string. Why?? sed -n '/<min-pool-size>/,/<\/min-pool-size>/p' Outputting:... (13 Replies)
Discussion started by: ArterialTool
13 Replies

8. Shell Programming and Scripting

SED to extract characters

Hi, Please let me know how do we write a piece of code to extract characters from a line using SED. (1 Reply)
Discussion started by: Shilpi
1 Replies

9. Shell Programming and Scripting

SED to extract characters

Hi, Please let me know how do we write a piece of code to extract characters from a line using SED. (2 Replies)
Discussion started by: Shilpi
2 Replies

10. Shell Programming and Scripting

Please help! Sed extract a pattern

I am trying to extract "securitySettings" out of line: <a ref ="http://localhost:5654/securitySettings"> using sed as follows: name = `grep "localhost" file.html | sed -n 's/.**\/\(.*)/\">/\1/p'` But it didn't run, seems have some syntax error. Do anybody knows why? Thank you very... (11 Replies)
Discussion started by: zhen
11 Replies

Featured Tech Videos