LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training 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 06-10-2009, 01:31 AM   #1
msgforsunil
Member
 
Registered: Jun 2006
Posts: 38

Rep: Reputation: 15
Parsing String.


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
 
Old 06-10-2009, 03:10 AM   #2
joel2001k
Member
 
Registered: Mar 2007
Distribution: GNU/Linux debian unstable main
Posts: 93

Rep: Reputation: 17
What programming language do you want to use?

May be the sscanf function in C
 
Old 06-10-2009, 08:25 AM   #3
nuwen52
Member
 
Registered: Feb 2009
Distribution: Debian, CentOS 5, Gentoo, FreeBSD, Fedora, Mint, Slackware64
Posts: 208

Rep: Reputation: 46
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 09:16 AM.
 
Old 06-10-2009, 10:52 AM   #4
fpmurphy
Member
 
Registered: Jan 2009
Location: /dev/ph
Distribution: Fedora, Ubuntu, Redhat, Centos
Posts: 299

Rep: Reputation: 62
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
 
Old 06-10-2009, 08:23 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,395

Rep: Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395
Perl; use 'split()' fn.
 
Old 06-10-2009, 09:10 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
some other solutions here

Last edited by ghostdog74; 06-10-2009 at 09:52 PM. Reason: rephrase
 
Old 06-10-2009, 09:20 PM   #7
jamescondron
Member
 
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961

Rep: Reputation: 69
No rule against asking across different sites, is there?
 
Old 06-10-2009, 09:52 PM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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.
 
Old 06-11-2009, 01:47 PM   #9
msgforsunil
Member
 
Registered: Jun 2006
Posts: 38

Original Poster
Rep: Reputation: 15
Thanks for your replies.
 
  


Reply

Tags
array


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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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