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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
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
|
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Rep: 
|
basename /usr/local/bin/firefox
|
|
|
09-12-2005, 07:21 AM
|
#3
|
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
|
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: 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.
|
|
|
All times are GMT -5. The time now is 10:37 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
|
|