LinuxQuestions.org
Help answer threads with 0 replies.
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 03-06-2005, 02:44 PM   #1
Ateo
Member
 
Registered: Sep 2004
Location: Long Beach, CA
Distribution: FreeBSD,Ubuntu,Gentoo,MacOS
Posts: 139

Rep: Reputation: 15
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.
 
Old 03-06-2005, 03:45 PM   #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
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.
 
Old 03-06-2005, 08:22 PM   #3
farmerjoe
Member
 
Registered: Oct 2004
Location: Texas
Distribution: Ubuntu - Home, RHEL4 - Server
Posts: 96

Rep: Reputation: 15
Another method is using cut:

variable="domain.tld/user"
domain=`echo "$variable" | cut -d"/" -f1`
user=`echo "$variable" | cut -d"/" -f2`
 
Old 03-08-2005, 03:57 PM   #4
Ateo
Member
 
Registered: Sep 2004
Location: Long Beach, CA
Distribution: FreeBSD,Ubuntu,Gentoo,MacOS
Posts: 139

Original Poster
Rep: Reputation: 15
Absolutely wonderful. Thanks!!!
 
Old 03-08-2005, 04:19 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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
 
Old 03-08-2005, 11:58 PM   #6
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

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


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
Kernel Assistance DigiCrime Linux - Software 13 11-01-2005 12:37 PM
Need assistance please wennie Linux - Software 5 03-16-2005 07:24 AM
bash assistance requested.. Ateo Programming 3 02-09-2005 06:41 PM
Need assistance spotslayer Linux - Software 1 11-18-2004 06:49 AM
Need Assistance Bizar Slackware 11 06-25-2003 09:10 PM

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

All times are GMT -5. The time now is 07:34 AM.

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