LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Little formatting help needed. (https://www.linuxquestions.org/questions/linux-newbie-8/little-formatting-help-needed-797188/)

pinga123 03-22-2010 11:49 PM

Little formatting help needed.
 
I have a following string.

"machine=IFLMUD5HP0581&group1=Stop"

I have created 2 variables namely machine and action.

machine should contain
IFLMUD5HP0581
action should contain
Stop

How do i write a script for the same.

grail 03-23-2010 01:24 AM

Code:

var="machine=IFLMUD5HP0581&group1=Stop"

machine=$( echo $var | sed 's/.*=\(.*\)\&.*/\1/' )
action=$( echo $var | sed 's/.*=\(.*\)/\1/' )


grail 03-23-2010 01:39 AM

Thought about this a little more and if there is a chance you could alter group1 to be action
or did not mind using group1 instead of action, you could also use this:

Code:

var="machine=IFLMUD5HP0581&group1=Stop"

eval $(echo $var | awk -F"&" '{ print $1"\n"$2 }')


catkin 03-23-2010 02:16 AM

Here's a method, demonstrated at the command prompt
Code:

c@CW8:~$ x="machine=IFLMUD5HP0581&group1=Stop"
c@CW8:~$ IFS='&'
c@CW8:~$ array=( $x )
c@CW8:~$ eval ${array[0]}
c@CW8:~$ echo $machine
IFLMUD5HP0581
c@CW8:~$ eval ${array[1]}
c@CW8:~$ echo $group1
Stop
unset IFS


grail 03-23-2010 03:06 AM

@catkin - I like it! :)

btw. on testing I found you could do: eval ${array[@]}

catkin 03-23-2010 03:09 AM

Quote:

Originally Posted by grail (Post 3908677)
on testing I found you could do: eval ${array[@]}

Neat! :)

pinga123 03-23-2010 06:04 AM

Thank you very much my problem is solved.

grail 03-23-2010 08:33 AM

pinga123 ... nice to see you have a solution, do not forget to mark as SOLVED using the Thread Tools


All times are GMT -5. The time now is 01:25 AM.