You are not logged in.
I guess this is more generic then sed but here's an example.
i want this:
test_string='"Name":"Foo","Description":"Totally the \"best\" app ever"'
echo "$test_string" | sed -e 's/.*"Name":"\([^"]*\)","Description":"\([^"]*\)".*/\1\n\2/g'
to output this:
Foo
Totally the \"best\" app ever
but it actually outputs this:
Foo
Totally the \
which is, of course, correct.
is there some flag i can pass or do i have to make the regexp ridiculously more complicated and illegible?
maybe in this specific case, i could 's/\\"/\'/g' before running the above but i was hoping there'd be something more generic and simple.
//github/
Offline
Inserting that first substitution into the sed script isn't really (much of) a complication.
You could also match up to the ",":
echo "$test_string" | sed -e 's/.*"Name":"\([^,]*\)","Description":"\([^,]*\)".*/\1\n\2/g'
As the shell will lose the outer ""
Or you could use awk.
"...one cannot be angry when one looks at a penguin." - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle
Offline
thanks skanky, this bit has been through many iterations from cut to awk to sed. seems the sed way is fastest. parsing on ',' is fine (and was actually how it was at one point) but then a ',' in the Description (escaped or otherwise) would cause trouble... i think i had an awk -F '","' version at one point...
anyway, i think the initial substitution will be the way i'll go. just wasn't sure if there was some --respect-escapes option i couldn't find.
/edit: oh well, atleast i can do it in a single sed call:
//blue/0/~/ cat ./foo.sh
#!/bin/bash
test_string='"Name":"Foo","Description":"Totally the \"best\" app ever"'
echo "$test_string" | sed -e 's/\\"/'\''/g' -e 's/.*"Name":"\([^"]*\)","Description":"\([^"]*\)".*/\1\n\2/g'
//blue/0/~/ ./foo.sh
Foo
Totally the 'best' app ever
guess that'll have to do.
Last edited by brisbin33 (2010-05-05 20:42:04)
//github/
Offline
Should have known you'd have thought of it...though I sometimes get stuck in a loop and miss the obvious.
TBH I've seen plenty of sed scripts that do multiple substitutions - the first one or two being to prepare for the main ones that come later, that I don't think twice to do it now (most of these are by people who know sed inside and out and backwards). At least sed only needs to be called once.
Check out some of the ones in O'Reilly's Sed & Awk book, for example.
Edit: this works too
sed -e 's/\"/'\''/g; s/.*"Name":"\([^"]*\)","Description":"\([^"]*\)".*/\1\2/g'
not much difference, and may be less readable.
In scripts, multi-line is also okay to overcome that.
sed -e 's/\"/'\''/g
s/.*"Name":"\([^"]*\)","Description":"\([^"]*\)".*/\1\2/g'
NB this is more for completeness and future reference for others, than anything.
Last edited by skanky (2010-05-05 20:58:39)
"...one cannot be angry when one looks at a penguin." - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle
Offline