Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 11-23-2012, 06:13 AM   #1
LQ Newbie
Registered: Aug 2012
Posts: 3

Rep: Reputation: Disabled
Problem with get data using sed command


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,
Nanthagopal A
Old 11-23-2012, 07:30 AM   #2
Senior Member
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,478

Rep: Reputation: 408Reputation: 408Reputation: 408Reputation: 408Reputation: 408
Have ...
"test":"test message", "testmessage":"subscription is active, charge successfully"
Want ...
"testmessage":"subscription is active, charge successfully"
Try this modification of your sed ...
sed -e 's/^.*\("testmessage":[^,]*\)\(.*\)/\1\2/'
If you always want all characters following the first comma, try cut instead of sed ...
cut -d"," -f2-
This is less complicated but has the minor disadvantage of delivering the desired string with a leading blank.

Daniel B. Martin
Old 11-23-2012, 09:07 AM   #3
Senior Member
Registered: Nov 2005
Distribution: Arch
Posts: 2,994

Rep: Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192
Shouldn't the assignment statement use single quotes:
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 #!):
./ line 2: message, testmessage:subscription: command not found
Originally Posted by danielbmartin
Have ...
"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:
sed -e 's/^.*\("testmessage":"[^"]*"\).*$/\1/'
## or
grep -o '"testmessage":"[^"]*"'
Old 11-23-2012, 09:21 AM   #4
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,147

Rep: Reputation: 2643Reputation: 2643Reputation: 2643Reputation: 2643Reputation: 2643Reputation: 2643Reputation: 2643Reputation: 2643Reputation: 2643Reputation: 2643Reputation: 2643
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.


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
Is sed a solution to this problem for loading data back into mysql db? newbie14 Linux - Newbie 14 12-23-2011 07:09 AM
Problem with SED command santhoshv Programming 1 04-03-2008 04:31 AM
sed command Problem gizmo1007 Linux - Newbie 2 02-18-2008 06:17 PM
SED command problem vikasumit Linux - General 8 07-06-2006 12:33 PM
problem with sed command LFS 6.0 obsidianblackha Linux From Scratch 6 12-07-2004 10:31 AM

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

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration