LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 08-08-2010, 10:06 AM   #1
forbin
Member
 
Registered: Apr 2010
Posts: 43

Rep: Reputation: 0
Bash gurus: Using 'cut' to assign multiple shell variables?


Suppose I have a variable containing a delimited string as follows:

MYSTRING="field1:field2:field3:field4"

Then suppose I want to assign the value of field1 within the string to a new variable. I would often do it like this...

MYVAR1=`echo $MYSTRING|cut -d":" -f1`

That works fine, but things get clumsy when I want to assign all of the fields within the string to their own variables, like this...

MYVAR1=`echo $MYSTRING|cut -d":" -f1`
MYVAR2=`echo $MYSTRING|cut -d":" -f2`
MYVAR3=`echo $MYSTRING|cut -d":" -f3`
MYVAR4=`echo $MYSTRING|cut -d":" -f4`

Is there an elegant way to assign all of the fields within MYSTRING to separate variables with a single command?
 
Old 08-08-2010, 10:13 AM   #2
forbin
Member
 
Registered: Apr 2010
Posts: 43

Original Poster
Rep: Reputation: 0
Better yet, suppose I just want fields 2 and 4? I'm aware that I can get just those fields with the cut command, as follows...

echo $MYSTRING|cut -d":" -f2,4

But how do I assign fields 2 and 4 to separate variables? That's the answer I'm really looking for.
 
Old 08-08-2010, 10:17 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
How about short sed:
Code:
MYVAR=$(echo $MYSTRING | sed 's/:/ /g')
Then simply reference each based on array location: eg. ${MYVAR[0]}
 
Old 08-08-2010, 10:27 AM   #4
forbin
Member
 
Registered: Apr 2010
Posts: 43

Original Poster
Rep: Reputation: 0
Well, dang. Once again I posted too soon. Found my answer. Sorry for the trouble.
 
Old 08-08-2010, 10:28 AM   #5
forbin
Member
 
Registered: Apr 2010
Posts: 43

Original Poster
Rep: Reputation: 0
Grail, that works, too. Maybe even better than the other solution I found. Thanks!
 
Old 08-08-2010, 10:29 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
No probs Please mark as SOLVED if you have your solution.
 
Old 08-08-2010, 10:36 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by grail View Post
How about short sed:
Code:
MYVAR=$(echo $MYSTRING | sed 's/:/ /g')
Then simply reference each based on array location: eg. ${MYVAR[0]}
Shouldn't that be: MYVAR=($(echo $MYSTRING | sed 's/:/ /g'))

Your code will just fill MYVAR (or MYVAR[0]) with the echoed input:
Code:
$ MYVAR=$(echo $MYSTRING | sed 's/:/ /g')
$ echo ${MYVAR}
field1 field2 field3 field4
$ echo ${MYVAR[0]}
field1 field2 field3 field4
$ echo ${MYVAR[1]}

$
Maybe bash version depended (tested with bash 3.1.1, no access to a 4 version atm).

Just curious.

Last edited by druuna; 08-08-2010 at 11:06 AM. Reason: Fixed a typo
 
Old 08-08-2010, 11:01 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
@druuna ... yes you are correct .. my bad .. I had actually thrown mine into a loop and forgot to then say an array needs the extra brackets.

good catch
 
Old 08-08-2010, 11:05 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405


Which does make me wonder:
Quote:
Originally Posted by forbin View Post
Grail, that works, too. Maybe even better than the other solution I found. Thanks!
 
Old 08-08-2010, 11:15 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Yeah I get the feeling the OP may have had it in a loop
 
  


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
How do I create multiple variables from a list in Bash? Passions Programming 12 05-23-2009 05:23 PM
Korn Shell test if multiple variables are set jonlake Programming 4 04-09-2008 10:41 AM
shell scripting/bash/variables mayaabboud Linux - Newbie 13 01-06-2008 12:16 PM
shell scripting/bash/variables mayaabboud Linux - General 4 01-06-2008 08:17 AM
bash: random creation of multiple variables vicious1 Programming 3 04-23-2007 03:34 AM

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

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