LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

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

Rep: Reputation: Disabled
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
 
Old 11-23-2012, 07:30 AM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,136

Rep: Reputation: 299Reputation: 299Reputation: 299
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
 
Old 11-23-2012, 09:07 AM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,494

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
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":"[^"]*"'
 
Old 11-23-2012, 09:21 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,564

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


Reply


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 07:05 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration