LinuxQuestions.org
Help answer threads with 0 replies.
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 09-21-2009, 04:37 AM   #1
ColInvictus
Member
 
Registered: Apr 2009
Posts: 42

Rep: Reputation: 15
Quick sed question


I have a series of files in the format:

{"field(1)":"value(1)","field(2)":"value(2), ... ,"field(n)","value(n)"}

Which I'm trying to split using sed to have each field/value pair on on line:

sed -e '1s/^.//' -e '$s/.$//' -e 's/,/\n/g'

So far so simple, but some of the values might have commas in them, which means they'll be split up. Is there a way to change this so that only commas not within quotes will be replaced with \n? (e.g. make sed count number of " and if it finds a , after counting an odd number of " then ignore it?)

Thanks
Col
 
Old 09-21-2009, 04:45 AM   #2
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
If it's exactly that, then you can replace runs of "," with "\n" and that will only match those commas between quotes rather than inside them.

Code:
:cat data
{"field(1)":"value(1)","field(2)":"value(2)","field(n)":"value(n)","field(z)":"comma,stuff"}

:sed -e '1s/^.//' -e '$s/.$//' -e 's/","/"\n"/g' data
"field(1)":"value(1)"
"field(2)":"value(2)"
"field(n)":"value(n)"
"field(z)":"comma,stuff"
Of course, if the data isn't that exact, then that may not work.

Last edited by slakmagik; 09-21-2009 at 04:46 AM.
 
Old 09-21-2009, 05:04 AM   #3
ColInvictus
Member
 
Registered: Apr 2009
Posts: 42

Original Poster
Rep: Reputation: 15
I never thought of that... Thanks!
 
  


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
quick sed question - how do you add a line after a match? BrianK Programming 2 09-16-2009 07:22 PM
Quick SED Script windisch Programming 7 06-07-2006 02:43 AM
Quick sed question aevangelica Linux - General 5 05-25-2006 10:55 AM
Quick sed question scuffell Linux - General 1 02-11-2005 05:02 PM
Quick question: quoting text in sed overbored Linux - Software 0 06-24-2004 01:23 PM

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

All times are GMT -5. The time now is 06:14 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