LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-22-2010, 11:49 PM   #1
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Rep: Reputation: 36
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.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 03-23-2010, 01:24 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,494

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Code:
var="machine=IFLMUD5HP0581&group1=Stop"

machine=$( echo $var | sed 's/.*=\(.*\)\&.*/\1/' )
action=$( echo $var | sed 's/.*=\(.*\)/\1/' )
 
1 members found this post helpful.
Old 03-23-2010, 01:39 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,494

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
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 }')
 
1 members found this post helpful.
Old 03-23-2010, 02:16 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
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
 
1 members found this post helpful.
Old 03-23-2010, 03:06 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,494

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
@catkin - I like it!

btw. on testing I found you could do: eval ${array[@]}
 
2 members found this post helpful.
Old 03-23-2010, 03:09 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by grail View Post
on testing I found you could do: eval ${array[@]}
Neat!
 
1 members found this post helpful.
Old 03-23-2010, 06:04 AM   #7
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Original Poster
Rep: Reputation: 36
Thank you very much my problem is solved.
 
Old 03-23-2010, 08:33 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,494

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


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
Output formatting help needed. pinga123 Linux - Newbie 8 11-03-2009 06:34 AM
XGL needed for Beryl is conflicting with OpenGL needed for Cedega chunkeydelight Linux - Desktop 2 07-18-2007 05:50 AM
Errors installing Q, which is needed for Lex, which is needed for PHP Virtuality Linux - Software 1 05-29-2007 04:47 PM
cannot use formatting ygloo LQ Suggestions & Feedback 2 09-28-2006 02:39 PM
Help needed in formatting an USB Harddrive vasanthd Linux - Laptop and Netbook 4 08-11-2005 10:25 AM


All times are GMT -5. The time now is 08:43 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration