LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 12-13-2002, 04:56 AM   #1
black
Member
 
Registered: Jul 2002
Location: nowhere
Distribution: GNU
Posts: 118

Rep: Reputation: 15
Question how to substr ?


Hi folks~

I touched shell programming not so long and now I need a way to get only part of a directory path. for example I have the folder as follows:
/black/goodie/games/warcraft
and what I expect is like this:
/black/goodie/games

and I knew in other programming languages there may be a function like substr could handle it and how about in shell(bash to me) ?

thanx in advance~
 
Old 12-13-2002, 05:13 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
well we can't say how to get from a to be here as we really have no idea of the context. do you want to remove the last directory? do you want to remove the word "warcraft"? whatever it is that you want will no doubt be acoomplished using sed and/or tr and/or cut, but without a proper scenario it's not possible to actually tell you the answer. cehck the manpages though, super bed time reading!
 
Old 12-13-2002, 05:33 AM   #3
black
Member
 
Registered: Jul 2002
Location: nowhere
Distribution: GNU
Posts: 118

Original Poster
Rep: Reputation: 15
oh I'll check sed and cut for more help~

I'd really appreciate your information, thanx acid_kewpie~
 
Old 12-16-2002, 10:46 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
If you have the full path of a file in a shell variable, say "$SOMEFILE",
you can take substrings from the variable by using sed, cut, awk, etc.
If you only want to take directory or file names from a full path, it's probably easier to use the "basename" or the "dirname" utility. See their man pages.

But the bash shell, can do some simple substrings itself. This is easier to learn, and it's faster. Some examples:

Code:
# asuming warcraft is a file, so there'll be no trailing '/'
SOMEFILE=/black/goodie/games/warcraft

# strip directory:
FILENAME=${SOMEFILE##*/}
# This means: Remove longest possible part ('##') from the
# beginning of variable SOMEFILE that matches the wildcard
# pattern ('*/').
# So FILENAME becomes the $SOMEFILE with all characters
# removed up to (and including) the last slash.
echo $FILENAME

# strip filename:
DIRNAME=${SOMEFILE%/*}
# This means: Remove the shortest possible part ('%') from the
# end that matches '/*'.
echo $DIRNAME

# Take a substring using character positions (zero based)
# This example takes out "goodie" from the path, by 
# taking the substring of 6 characters, starting with character
# number 7:
TMP=${SOMEFILE:7:6}
echo $TMP

Last edited by Hko; 12-16-2002 at 11:02 AM.
 
Old 12-16-2002, 05:09 PM   #5
lackluster
Member
 
Registered: Apr 2002
Location: D.C - USA
Distribution: slackware-current
Posts: 488

Rep: Reputation: 30
thanks, Hko that was really helpful.
 
Old 12-23-2002, 12:39 AM   #6
tana
LQ Newbie
 
Registered: Dec 2002
Distribution: Mandrake 8.2
Posts: 4

Rep: Reputation: 0
An excellant reference for such shell scripting can be found in Chapter 9.2 of the Advanced Bash - Scripting Guide at the Linux Documentation Project (http://www.tldp.org/guides.html).
 
  


Reply



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
perl: using 'substr' in regex ananthbv Programming 5 11-03-2004 01:58 AM
PHP (substr) & UTF-8 liquid sky Programming 3 09-08-2004 12:26 PM
C++: char* to string, converting? rewriting program? need substr! lrt2003 Programming 2 06-19-2004 03:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:10 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration