Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 09-12-2005, 12:01 AM   #1
LQ Newbie
Registered: Oct 2004
Location: korea
Posts: 11

Rep: Reputation: 0
(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.

Old 09-12-2005, 12:09 AM   #2
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
basename /usr/local/bin/firefox
Old 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
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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.
Old 09-12-2005, 08:42 AM   #4
Senior Member
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
Or sed:
sed 's|^.*/||' <<<'/usr/local/bin/firefox'
Or bash :
echo "${ffpath##*/}"
Old 09-12-2005, 07:59 PM   #5
Senior Member
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
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
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
Shell Script - String Replacement revof11 Programming 7 11-29-2005 06:38 AM
Shell script to find a particular string Prasun1 Linux - General 5 08-30-2005 09:23 AM
Script File: Parsing command sent to shell cheema Programming 1 07-01-2005 12:54 PM
change case of a string (TOUPPER) in shell script raees Linux - Software 3 05-03-2005 02:13 PM
File Parsing using a Shell Script yasir15 Programming 5 08-22-2003 12:17 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:37 PM.

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