LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
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, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
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
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:16 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration