LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-15-2005, 06:52 AM   #1
dave bean
Member
 
Registered: Jun 2003
Location: UK
Distribution: Slackware 9.1
Posts: 136

Rep: Reputation: 15
bash + string manipulation


Hi
I have the following string stored in a variable:

Code:
[Requesting program interpreter: /lib/ld-linux.so.2]
I want to isolate ld-linux.so.2 and store this value in a variable, I don't want to rely on using character numbers to make a substring becasue the text part may vary from distro to distro, i want to seach the string until '/lib/' then cut out the linker (ld-linux.so.2) finishing at ']'

How would i go about this and what tool ie grep, sed, etc should i use ?

thanks
 
Old 02-15-2005, 07:15 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

This is one way, using sed:

echo "[Requesting program interpreter: /lib/ld-linux.so.2]" | sed 's%.*/lib/\(.*\)]%\1%'

Hope this helps
 
Old 02-15-2005, 09:38 AM   #3
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
I like the sed solution, but here it is with cut and basename:
Code:
basename `echo "[Requesting program interpreter: /lib/ld-linux.so.2]" |  cut -d: -f2 | cut -d']' -f1 `
 
Old 02-15-2005, 10:01 AM   #4
LasseW
Member
 
Registered: Oct 2004
Distribution: Fedora 7, OpenSuse 10.2
Posts: 108

Rep: Reputation: 15
Or use expr with a regexp. This is very similar to druna's solution:

$ VAR1='[Requesting program interpreter: /lib/ld-linux.so.2]'
$ VAR2=$(expr "$VAR1" : '.*/lib/\(.*\)]')
$ echo $VAR2
ld-linux.so.2
 
Old 02-15-2005, 11:35 AM   #5
dave bean
Member
 
Registered: Jun 2003
Location: UK
Distribution: Slackware 9.1
Posts: 136

Original Poster
Rep: Reputation: 15
Hi
Thanks for the replies, I used sed finally, cos i understood the command and im trying to get to grips with it

crabboy, would you mind explaining how your command works, i took a look at the info pages of cut
and basename but i still don't understand it

The other thing is, i was wondering if theres a way to simulate the running of a shell script to ascertain if there are bugs etc
I don't want half the script to execute and then crash out leaving everything half done.

thanks
 
Old 02-16-2005, 07:35 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

If you need any (extra) help with the sed command, just let me (us) know.

About the command crabboy gave (don't think he minds me explaining it):

basename `echo "[Requesting program interpreter: /lib/ld-linux.so.2]" | cut -d: -f2 | cut -d']' -f1 `

basename will strip directories and suffix from filename so basename /path/to/program will give you program.

So only the /lib/ld-linux.so.2 part of the variable you gave ( [Requesting program interpreter: /lib/ld-linux.so.2] ) should be given to basename. The echo and the 2 cut statements do this.

cut -d: -f2 sets the delimiter to : and prints the second (-f2) field. You will end up with:

/lib/ld-linux.so.2]

Looks nice, but there's still a ] at the end of the string. The leading space(s) can be ignored.

cut -d']' -f1 sets the delimiter to ] ( -d']' ) and print the first field ( -f1 ). You will end up with:
/lib/ld-linux.so.2

Because the part between ` and ` (backquotes) will be done first by the shell the command after the execution of the `echo|cut|cut` commands looks like this:

basename /lib/ld-linux.so.2, which in term prints: ld-linux.so.2

Hope this clears things up a bit.
 
Old 02-16-2005, 10:10 AM   #7
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
Thanks druuna, well done.
 
Old 02-16-2005, 11:16 AM   #8
dave bean
Member
 
Registered: Jun 2003
Location: UK
Distribution: Slackware 9.1
Posts: 136

Original Poster
Rep: Reputation: 15
ahh, thanks a lot druuna, that was good of you

I also sussed what you was talking about before about using 'sh -x a.sh' and 'sh -n a.sh'

thanks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash way to tell if String is in String tongar Programming 3 06-16-2005 06:59 AM
Filename string manipulation jango77 Linux - Newbie 2 03-27-2005 06:58 PM
String manipulation with a script. philipina Programming 4 03-16-2004 02:42 PM
String manipulation with a script? philipina General 1 03-15-2004 12:07 PM
BASH string expansions Barbarian Programming 2 11-21-2001 10:40 PM


All times are GMT -5. The time now is 08:53 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration