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
perl code, one possibility:
my $string = 'attr1=value1 attr2=value2 attr3=value3 attr4=value4';
my $attr = 'attr2';
my ($value) = $string =~ /$attr=([^ ]+) /;
print $value;
if you have Python
#!/usr/bin/env python
d={}
string='attr1=value1 attr2=value2 attr3=value3 attr4=value4'
for i in string.split():
attr,val = i.split("=")
d[attr]=val
choice = raw_input("Enter attribute: ")
if d.has_key(choice):
print d[choice]
else:
print "No such attribute: %s " % choice
output
# ./test.py
Enter attribute: attr
No such attribute: attr
# ./test.py
Enter attribute: attr2
value2
Thank you very much for the quick reply.
Unfortunately, I am not using phython and I am doing using ksh.
And the below statements are not working for me
my $string = 'attr1=value1 attr2=value2 attr3=value3 attr4=value4';
my $attr = 'attr2';
my ($value) = $string =~ /$attr=([^ ]+) /;
print $value;
For eg:
<user1@test>my $string = 'attr1=value1 attr2=value2 attr3=value3 attr4=value4';
ksh: string: parameter not set
I did say my code was perl
Note sure what I am missing. I tried like below and got the error
<user1@test>perl $string = 'attr1=value1 attr2=value2 attr3=value3 attr4=value4';
ksh: string: parameter not set
Since, I am using ksh as the shell, I am tried as below.
<user1@test>string="attr1=value1 attr2=value2 attr3=value3 attr4=value4"
<user1@test>attr="attr2"
<user1@test>perl ($value) = $string =~ /$attr=([^ ]+) /;
ksh: syntax error: `(' unexpected
<user1@test>
Please let me know on this.
Thanks for your quick reply.
Sunil Kumar
awk
awk 'BEGIN{
string ="attr1=value1 attr2=value2 attr3=value3 attr4=value4"
m=split(string,str," ")
printf "Enter attribute: "
getline choice <"/dev/stdin"
for (i=1;i<=m;i++){
if ( str ~ choice){
split(str,a,"=")
print "value is " a[2]
}
}
}'
hope below shell can help you some
find=$1
a='attr1=value1 attr2=value2 attr3=value3 attr4=value4'
set $a
while [ $# -gt 0 ];do
key=`echo ${1%=*}`
val=`echo ${1#*=}`
echo $key "--" $val
if [ $key = $find ];then
echo $val
break
fi
shift
done
echo "Finish searching..."
If you are using ksh93 you could use associative arrays
#!/usr/bin/ksh93
typeset -A aArray
str="attr1=value1 attr2=value2 attr3=value3 attr4=value4"
# split string and store in temporary array
IFS="= " typeset -a tArray=($str)
# populate associative array
for ((i = 0; i < ${#tArray[@]}; i++))
do
aArray[${tArray}]=${tArray[++i]}
done
# print contents of associative array
for i in "${!aArray[@]}"
do
print "aArray[$i] is ${aArray[$i]}"
done
There's no need for any external command:
string="attr1=value1 attr2=value2 attr3=value3 attr4=value4"
eval "$string"
echo "$attr2" ## prints 'value2'
Or:
string"attr1=value1 attr2=value2 attr3=value3 attr4=value4"
name=attr2
temp=${string#*"$name"=}
echo "${temp%% *}"
...or various other methods.
Thank you very much for all your responses.
-----Post Update-----
Curious as what changes should I do in the below script inorder to handle cases that contains values vales with spaces in it?
for eg. string"attr1=value1 attr2=value 2 attr3=value3 attr4=value4"
string"attr1=value1 attr2=value2 attr3=value3 attr4=value4"
name=attr2
temp=${string#*"$name"=}
echo "${temp%% *}"
-----Post Update-----
Curious as what changes should I do in the below script inorder to handle cases that contains values vales with spaces in it?
for eg. string"attr1=value1 attr2=value 2 attr3=value3 attr4=value4"
string"attr1=value1 attr2=value2 attr3=value3 attr4=value4"
name=attr2
temp=${string#*"$name"=}
echo "${temp%% *}"