Problem with filter data using sed command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with filter data using sed command
# 1  
Old 11-23-2012
Problem with filter 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 : "testmessage":"subscription is active

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

Please Suggest me,
Regards,
Nanthagopal A

Moderator's Comments:
Mod Comment Use code tags - see PM, thanks.

Last edited by zaxxon; 11-23-2012 at 07:57 AM.. Reason: code tags
# 2  
Old 11-23-2012
Try:
Code:
status=$( echo $String | sed -e 's/^.*\("testmessage":"[^"]*"\).*$/\1/')

# 3  
Old 11-23-2012
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:

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

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

Code:
echo "$String"
echo $String

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

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Including Hash / in sed command filter

Hello All, I want to print data in between two lines in a file sample.txt through more or cat command on the screen. For that I am using below sed command to give the BEGIN and END text. Content of sample.txt server01:~ # cat /proc/mdstat Hello this is a text message 1 Hello this is a... (5 Replies)
Discussion started by: Xtreme
5 Replies

2. Shell Programming and Scripting

Problem in extracting data using cut/awk command

Hi Everyone, I have a very simple problem and i am stuck in that from last 8 days. I tried many attempts, googled my query but all in vain. I have a text file named "test.txt" In that suppose i have contents like: Java: 1 Object oriented programming language 2 Concepts of Abstraction... (5 Replies)
Discussion started by: Abhijeet Anand
5 Replies

3. Shell Programming and Scripting

How to grep/sed selected data from a command or file?

Below is the output of a DB2 command. Now I have 2 requirements... Database Partition 0 -- Database TESTDB1 -- Active Standby -- Up 213 days 02:33:07 -- Date 02/22/2016 17:04:50 HADR Information: Role State SyncMode HeartBeatsMissed LogGapRunAvg (bytes) Standby ... (2 Replies)
Discussion started by: rlokesh27
2 Replies

4. UNIX for Dummies Questions & Answers

Command line / script option to filter a data set by values of one column

Hi all! I have a data set in this tab separated format : Label, Value1, Value2 An instance is "data.txt" : 0 1 1 -1 2 3 0 2 2 I would like to parse this data set and generate two files, one that has only data with the label 0 and the other with label -1, so my outputs should be, for... (1 Reply)
Discussion started by: gnat01
1 Replies

5. UNIX for Dummies Questions & Answers

Need to filter data from a file

Hi, I have a file with hundreds of records. There are four fields on each line, separated by semicolons. Name Height (meters) Country Continent (Africa,Asia,Europe,North America,Oceania,South America,The Poles) I need to Write the command to find display how many mountains appear... (1 Reply)
Discussion started by: erora
1 Replies

6. Shell Programming and Scripting

Filter date and time form a file using sed command

I want to filter out the date and time from this line in a file. How to do this using sed command. on Tue Apr 19 00:48:29 2011 (12 Replies)
Discussion started by: vineet.dhingra
12 Replies

7. Linux

filter data

hi all, here is my question. i have the following data Block 1 a b c d e f g h i Block 2 j k l m n o p q r i need to grep all information from block 1 i.e 'a to i' (6 Replies)
Discussion started by: yprudent
6 Replies

8. UNIX for Dummies Questions & Answers

How to filter data

Hi all, I require assistance in this. I'd like to filter the following data to extract WADRAIN. RAIN, 2003-01-01 00:00, 1, WADRAIN, 900, 1, 24109, 1011, 1.6, 0, 6, RAIN, 2003-01-01 00:00, 1, DLY3208, 900, 1, 30747, 1011, 1.8, 0, 6, RAIN, 2003-01-01 00:00, 1, WADRAIN, 900, 1, 24775,... (6 Replies)
Discussion started by: Muhammad Rahiz
6 Replies

9. Shell Programming and Scripting

extract data from a data matrix with filter criteria

Here is what old matrix look like, IDs X1 X2 Y1 Y2 10914061 -0.364613333 -0.362922333 0.001691 -0.450094667 10855062 0.845956333 0.860396667 0.014440333 1.483899333... (7 Replies)
Discussion started by: ssshen
7 Replies

10. UNIX for Dummies Questions & Answers

filter data

Hi I have two files and their names are donotcall.txt, filter_it.txt. I want output.txt file with all data that is in filter_it.txt file but not in donotcall.txt. Can anybody help me to write 1-2 lines unix code for this. donotcall.txt contains following data. Each row represent phone... (3 Replies)
Discussion started by: deep.singh
3 Replies
Login or Register to Ask a Question