hhanzo1
hhanzo1

Reputation: 647

Insert with sed

I start with this:

{"Allowed","20863962"}

And would like to insert label: and value: so it looks like this:

{ label: "Allowed", value: "20863962" }

I tried with sed, but it does a replace, instead of an insert.

echo "{"Allowed","20863962"}" | sed 's/A/label: /' | sed 's/[0-9]/value: /'

Output

{label: llowed,value: 0863962}

Upvotes: 1

Views: 122

Answers (3)

Beta
Beta

Reputation: 99094

echo '{"Allowed","20863962"}' | sed -e 's/{/{label: /' -e 's/,/, value: /'

Upvotes: 2

Jonathan Leffler
Jonathan Leffler

Reputation: 753615

You got what you asked for, but that wasn't what you intended.

First off, the output of the echo is:

{Allowed,20863962}

because the shell strips the double quotes out. To get the correct input to sed, use:

echo '{"Allowed","20863962"}'

Then your sed command drops characters, not to mention not handling the double quotes, and being highly specific to your current data. A more general solution would be:

sed 's/{\("[^"]*"\),\("[^"]*"\)}/{ label: \1, value: \2 }/'

This looks for two strings inside double quotes within { and } with a comma separating them and saves the strings (using \(...\)) for use as \1 and \2.

$ echo '{"Allowed","20863962"}'| sed 's/{\("[^"]*"\),\("[^"]*"\)}/{ label: \1, value: \2 }/'
{ label: "Allowed", value: "20863962" }
$

If the general regex is too general, a simpler technique is to use multiple replacements, where you can decide exactly how much importance you attach to the space after the open brace and, more particularly, before the close brace:

$ echo '{"Allowed","20863962"}' |
> sed -e 's/{/{ label: /' -e 's/,/, value: /' -e 's/}/ }/'
{ label: "Allowed", value: "20863962" }
$

The difference between the two is in the number of substitute operations and their complexity. In a sense, the single-substitute operation concentrates on the strings and fixes up their surroundings; the multiple-substitute operation concentrates on the surroundings and fixes those up.


We could 'rescue' your code using this. Note that there is no reason to run sed twice in this context.

sed -e 's/"A/label: &/' -e 's/"[0-9]/value: &/'

This finds the double quote and the A and replaces it with label: "A because the & means 'what was matched'. Similarly with the second replacement. This does not insert a space after the { or before the }, but that may be OK.

$ echo '{"Allowed","20863962"}' | sed -e 's/"A/label: &/' -e 's/"[0-9]/value: &/'
{label: "Allowed",value: "20863962"}
$

Were I using this format, I'd omit the spaces after the colons, but that's me...

Upvotes: 7

anubhava
anubhava

Reputation: 785058

Using awk:

awk -F "," '{sub(/"/, "label: \"", $1); sub(/"/, "value: \"", $2); print}' OFS=","

OUTPUT:

{label: "Allowed",value: "20863962"}

Upvotes: 1

Related Questions