LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Problem with get data using sed command (https://www.linuxquestions.org/questions/programming-9/problem-with-get-data-using-sed-command-4175438417/)

nanthagopal 11-23-2012 06:13 AM

Problem with get data using sed command
 
Hi,

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


Code:

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 :
Code:

"testmessage":"subscription is active
Expected output:
Code:

"testmessage":"subscription is active, charge successfully"
Please Suggest me,
Regards,
Nanthagopal A

danielbmartin 11-23-2012 07:30 AM

Have ...
Code:

"test":"test message", "testmessage":"subscription is active, charge successfully"
Want ...
Code:

"testmessage":"subscription is active, charge successfully"
Try this modification of your sed ...
Code:

sed -e 's/^.*\("testmessage":[^,]*\)\(.*\)/\1\2/'
If you always want all characters following the first comma, try cut instead of sed ...
Code:

cut -d"," -f2-
This is less complicated but has the minor disadvantage of delivering the desired string with a leading blank.

Daniel B. Martin

ntubski 11-23-2012 09:07 AM

Shouldn't the assignment statement use single quotes:
Code:

String='{ "test":"test message", "testmessage":"subscription is active, charge successfully" }'
Without that change I get an error (line 2 is the assignment, line 1 being the #!):
Code:

./keyval.sh: line 2: message, testmessage:subscription: command not found
Quote:

Originally Posted by danielbmartin
Have ...
Code:

"test":"test message", "testmessage":"subscription is active, charge successfully"

The initial post is a bit mangled, but it seems to me that the curly braces were intended to be part of the string.

I would try looking for the closing quote:
Code:

sed -e 's/^.*\("testmessage":"[^"]*"\).*$/\1/'
## or
grep -o '"testmessage":"[^"]*"'


grail 11-23-2012 09:21 AM

I agree with ntubski ... please carefully re-write the string as its current incarnation cannot be solved using the suggestions available due to extra curly brace.


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