LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux > Linux - Newbie
User Name
Password
Linux - Newbie This 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

Tags used in this thread
Popular LQ Tags , ,

Reply
 
Thread Tools
Old 06-10-2009, 02:31 AM   #1
msgforsunil
LQ Newbie
 
Registered: Jun 2006
Posts: 24
Thanked: 0
Parsing String.


[Log in to get rid of this advertisement]
Hello All,

I have a case, wherein I have a string of the format
"attr1=value1 attr2=value2 attr3=value3 attr4=value4"

How do I extract the value associated with for a given attributename. For eg. I need to get a value of "value2" when I give an input for attribute name as "attr2". Note, each attribute values is space seperated and value can contain / or , or - as part of it.

Thanks
Sunil Kumar
msgforsunil is offline     Reply With Quote
Old 06-10-2009, 04:10 AM   #2
joel2001k
Member
 
Registered: Mar 2007
Distribution: GNU/Linux debian unstable main
Posts: 64
Thanked: 1
What programming language do you want to use?

May be the sscanf function in C
joel2001k is offline     Reply With Quote
Old 06-10-2009, 09:25 AM   #3
nuwen52
Member
 
Registered: Feb 2009
Distribution: CentOS 5, Gentoo, FreeBSD, Fedora, Mint
Posts: 136
Thanked: 22
The language choice is useful here.

But, I'll add my two cents anyway. In python, you can split the string (based on " ") and feed that into a list comprehension (splitting the resulting strings based on "=") with an output to the dict function. This can be done in 1 line and gives a dictionary with values indexed by attributes.

This works with any number of key=value pairs in the string.

Last edited by nuwen52; 06-10-2009 at 10:16 AM..
nuwen52 is offline     Reply With Quote
Old 06-10-2009, 11:52 AM   #4
fpmurphy
Member
 
Registered: Jan 2009
Location: Florida
Distribution: Fedora, Ubuntu, Redhat, Centos
Posts: 181
Thanked: 24
One way is to use an associative array if your shell supports the concept.
Code:
#!/usr/bin/ksh93

typeset -A aArray

str="attr1=value1 attr2=value2 attr3=value3 attr4=value4"


# split the string and store in temporary array
IFS="= " typeset -a tArray=($str)

# populate associative array
for ((i = 0; i < ${#tArray[@]}; i++))
do
     aArray[${tArray[i]}]=${tArray[++i]}
done

# print contents of associative array
for i in "${!aArray[@]}"
do
    print "aArray[$i] is ${aArray[$i]}"
done
The output from running this script is:
Code:
aArray[attr1] is value1
aArray[attr2] is value2
aArray[attr3] is value3
aArray[attr4] is value4
fpmurphy is offline  
Tag This Post , ,
Reply With Quote
Old 06-10-2009, 09:23 PM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 5.4
Posts: 7,429
Thanked: 325
Perl; use 'split()' fn.
chrism01 is offline     Reply With Quote
Old 06-10-2009, 10:10 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 1,814
Blog Entries: 5
Thanked: 115
some other solutions here

Last edited by ghostdog74; 06-10-2009 at 10:52 PM.. Reason: rephrase
ghostdog74 is offline     Reply With Quote
Old 06-10-2009, 10:20 PM   #7
jamescondron
Member
 
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 561
Thanked: 37
No rule against asking across different sites, is there?
jamescondron is offline     Reply With Quote
Old 06-10-2009, 10:52 PM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 1,814
Blog Entries: 5
Thanked: 115
Quote:
Originally Posted by jamescondron View Post
No rule against asking across different sites, is there?
I am merely posting to inform (not just to OP) of other solutions as some of the people in other forums might not be a member here. So some good solutions might be missed out.
ghostdog74 is offline     Reply With Quote
Old 06-11-2009, 02:47 PM   #9
msgforsunil
LQ Newbie
 
Registered: Jun 2006
Posts: 24
Thanked: 0

Original Poster
Thanks for your replies.
msgforsunil is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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
Programming in BASH - Parsing a String Mistro116@yahoo.com Programming 9 06-18-2008 07:43 PM
string parsing using perl bharatbsharma Programming 1 12-05-2007 07:04 AM
(shell script) string parsing kuru Programming 4 09-12-2005 08:59 PM
parsing a user input string daphne19 Programming 1 04-22-2004 08:40 AM
Parsing a file for a string of text jamesmwlv Linux - General 2 12-02-2002 08:13 PM


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

Main Menu
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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration