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.
|
 |
03-06-2005, 02:44 PM
|
#1
|
Member
Registered: Sep 2004
Location: Long Beach, CA
Distribution: FreeBSD,Ubuntu,Gentoo,MacOS
Posts: 139
Rep:
|
Bash assistance
Hi. I'm not sure how to create 2 variables that I need to create.
I'm setting this variable:
variable="domain.tld/user"
From that, I need $variable split into 2 other variables (splitting at the slash). A "$domain" variable and a "$user" variable.
Is this possible?
Last edited by Ateo; 03-06-2005 at 02:53 PM.
|
|
|
03-06-2005, 03:45 PM
|
#2
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417
|
as ever there are a trillion ways to do this, try awk:
variable="domain.tld/user"
domain=$(echo $VAR | awk '{split($0,a,"/"); print a[1]}')
user=$(echo $VAR | awk '{split($0,a,"/"); print a[2]}')
i'm sure sed and/ord grep can do it too, no problem.
|
|
|
03-06-2005, 08:22 PM
|
#3
|
Member
Registered: Oct 2004
Location: Texas
Distribution: Ubuntu - Home, RHEL4 - Server
Posts: 96
Rep:
|
Another method is using cut:
variable="domain.tld/user"
domain=`echo "$variable" | cut -d"/" -f1`
user=`echo "$variable" | cut -d"/" -f2`
|
|
|
03-08-2005, 03:57 PM
|
#4
|
Member
Registered: Sep 2004
Location: Long Beach, CA
Distribution: FreeBSD,Ubuntu,Gentoo,MacOS
Posts: 139
Original Poster
Rep:
|
Absolutely wonderful. Thanks!!!
|
|
|
03-08-2005, 04:19 PM
|
#5
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536
Rep: 
|
Simpler and faster (no external programs are run, bash internal):
Code:
variable="domain.tld/user"
domain=${variable%/*}
user=${variable#*/}
echo Domain: $domain
echo User: $user
|
|
|
03-08-2005, 11:58 PM
|
#6
|
Member
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153
Rep:
|
All built-ins
Code:
SF1B : /supmis/soumen/tmp > cat st
variable="domain.tld/user"
set `IFS=/ ; echo $variable`
domain=$1 ; user=$2
echo "[domain:$domain user:$user]"
SF1B : /supmis/soumen/tmp > sh st
[domain:domain.tld user:user]
SF1B : /supmis/soumen/tmp > ksh st
[domain:domain.tld user:user]
SF1B : /supmis/soumen/tmp > bash st
[domain:domain.tld user:user]
SF1B : /supmis/soumen/tmp >
Except
Code:
SF1B : /supmis/soumen/tmp > csh st
variable=domain.tld/user: Command not found
set: Syntax error
SF1B : /supmis/soumen/tmp >
Good fun.
HTH.
|
|
|
All times are GMT -5. The time now is 07:34 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
|
|