Cannot extract libraries using sed


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Cannot extract libraries using sed
# 8  
Old 08-15-2019
Hi MadeinGermany,

Thanks for the responses, nice idea(s). The line without => is not captured via your regex, however DonCragun has provided some details around this.

Code:
 /lib64/ld-linux-x86-64.so.2 (0x00007fc21e165000)

Hi DonCragun,

Thanks for the response.

The initial solution with awk/tr/sort works. Looks like it is successful as the lines which do not have => as delimiter are left untouched by tr anyway.

Code:
./copy_chroot_lib.sh ls echo | tr '=>' '\n' | awk '/\/lib/{print $1}' | sort -u
/lib64/ld-linux-x86-64.so.2   <------------ line without => is included
/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

The awk solution is nice and works, as I only need to remove duplicates. Sed is also good, but a little complex to understand.

Can you please explain the following awk code?

Code:
 ./copy_chroot_lib.sh ls echo | awk '$(NF-1) ~ "^/lib" {o[$(NF- 1)]} END {for(f in o) print f}'

From what I understand, if the second last field starts with /lib, the field is put into an array named o. Then the array is traversed and the value of all array elements printed out. How does this remove duplicates?

Thanks.
# 9  
Old 08-15-2019
The duplicates are removed because of the associative (string-indexed) array o[ ].
If the string $(NF-1) occurs the second time it defines the same array element again.
You can make it visible by assigning a counter value to the array element, and printing it at the end:
Code:
awk '$(NF-1) ~ "^/lib" {o[$(NF- 1)]++} END {for(f in o) print f, "==>", o[f]}'

These 2 Users Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

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
Login or Register to Ask a Question