LinuxQuestions.org
Review your favorite Linux distribution.
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 09-12-2005, 12:01 AM   #1
kuru
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.

thanks!
 
Old 09-12-2005, 12:09 AM   #2
paulsm4
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
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,995
Blog Entries: 5

Rep: Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782
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
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

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

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


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
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


All times are GMT -5. The time now is 05:19 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