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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
09-12-2005, 12:01 AM
|
#1
|
|
LQ Newbie
Registered: Oct 2004
Location: korea
Posts: 11
Rep:
|
(shell script) string parsing
I am writing simple born shell script.
How can I extract 'firefox' from '/usr/local/bin/firefox' string ??
I don't care to use any tools just like 'gawk', 'sed' and etc.
thanks!
|
|
|
|
09-12-2005, 12:09 AM
|
#2
|
|
Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,861
Rep: 
|
basename /usr/local/bin/firefox
|
|
|
|
09-12-2005, 07:21 AM
|
#3
|
|
Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,644
|
Prior poster was correct - the basename command gives you the end of a fully qualified path (the files base name without its path). Similarly there is a dirname command that would give you the path without the base name.
However if you'd wanted to extract something out of the path you would have need something like awk. In awk you can change the default field separator (which is white space) with the -F flag/ so you could do:
echo /usr/local/bin/firefox |awk -F/ '{print $NF}'
NF is a predefined value meaning number of fields - by putting a $ in front of it you tell the print to print the last field. $1 would be the first (null as there is nothing in front of the first separator), $2 the second (usr), $3 the third (local), $4 the fourth (bin) and $5 the fifth (firefox). In this case NF = 5 so $NF is the same as $5.
|
|
|
|
09-12-2005, 08:42 AM
|
#4
|
|
Senior Member
Registered: Jul 2004
Location: Nantes (France)
Distribution: Arch Linux
Posts: 1,897
Rep:
|
Or sed:
Code:
sed 's|^.*/||' <<<'/usr/local/bin/firefox'
Or bash  :
Code:
ffpath='/usr/local/bin/firefox'
echo "${ffpath##*/}"
Yves.
|
|
|
|
09-12-2005, 07:59 PM
|
#5
|
|
Senior Member
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
|
BTW, it's a "Bourne" shell, after the original author.
The improved ver. we use now, "bash", is the "Bourne again shell"
Why not sed or awk? They are very useful & almost essential to anything beyond elementary in shell scripting.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 09:06 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|