Problem with filter data using sed command

Hi,

I am using the following command(sed) to get the key/value pair from the string

String="{ "test":"test message", "testmessage":"subscription is active, charge successfully} " }"
status=$( echo $String | sed -e 's/^.*\("testmessage":[^,]*\).*$/\1/')
echo $status

i am getting this output : "testmessage":"subscription is active

Expected output: "testmessage":"subscription is active, charge successfully"

Please Suggest me,
Regards,
Nanthagopal A

Try:

status=$( echo $String | sed -e 's/^.*\("testmessage":"[^"]*"\).*$/\1/')

First off: quotes cannot be nested. The shell maintains a "switch", so to say, if inside a quote or not. If it encounters a quote char reading the input it toggles that switch, if it encounters another, it toggles it again. Therefore your first line doesn't look to the shell as you probably believe it does. If you want to have literal double quotes inside a double quoted string you would have to escape them:

String="{ \"test\":\"test message\", \"testmessage\":\"subscription is active, charge successfully} \" }"

Second: your definition of what "ends a message" is sloppy. You (correctly) recognize "," as a "message end", because it would start a new message (or message field, ot clear from your example). But this is not true for the last message, which ends with the "line delimiter", a curly bracket. You will have to search for this one too:

status=$( echo $String | sed -e 's/^.*\("testmessage":[^},]*\).*$/\1/')

A last detail: if you use "$String" unquoted, as you do, your double quotes are "cooked" by the shell, even the escaped ones. In fact the string is parsed two times by the shell, the first time in your variable definition (there the two outside double quotes are stripped off) and then by the "echo"-statement, where the now unprotected quote chars are stripped. This is the difference between these lines:

echo "$String"
echo $String

Therefore your sed-statement, which matches against double quotes will probably not work at all.

I hope this helps.

bakunin