13 ms·
> then I'd have to write a fairly complex sed(1) command to first remove the path No need for sed for the path, use cut for that as well. cut -d'=' -f2- will
by kbenson 9y ago
> then I'd have to write a fairly complex sed(1) command to first remove the path
No need for sed for the path, use cut for that as well. cut -d'=' -f2- will remove the path (but leave a space).
In the end, you can accomplish it with the following, whichI think is fairly easy:
echo 'json[0].foo.bar.baz = "some string";' | cut -d'=' -f2 | sed -e 's/^\s*"//' -e 's/";$//'
For me, it's a toss up whether I would use that or Perl, since chances are I'm doing it as a first step in some other process, and I can just continue on in Perl for the rest of the process anyway.
echo 'json[0].foo.bar.baz = "some string";' | perl -pE 's/^.*?"//; s/";$//;'
I find keeping the output as valid JS extremely useful though, since I can just paste a grepped entry into a developer console to get a valid object to play with on a page. That's cutting out a pipe to a js prettifier, pipe to less, search for identifying text, and careful cut and paste to get the enclosing block of text for what ends up being a semi-common action for me. On the other hand, I can get raw strings, but barely ever have need of that, and could fairly easily make an alias for that if it became common.
- cryptonector 9y agoA single grep and cut would be simpler. You've proved my point :)