0

Will this be possible in sed ?

I want to replace , in tuple start with [ and end with ] to |

"platform": "iPhone","report_attrs": {"SKU": "XYZ123","Unit Price": "32.25","Qty": "3"},"screen_flow":["Main","Login"] "platform": "android","report_attrs": {"SKU": "XYZ123","Unit Price": "32.25","Qty": "3"} 

output :

"platform": "iPhone","report_attrs": {"SKU": "XYZ123","Unit Price": "32.25","Qty": "3"},"screen_flow":["Main"|"Login"] "platform": "android","report_attrs": {"SKU": "XYZ123","Unit Price": "32.25","Qty": "3"} 

If not possible any other solution will be appreciated.

Thank you.

2
  • Is the array always in the last value, or can there be several arrays? Commented Aug 20, 2014 at 11:47
  • there can be several arrays in middle too Commented Aug 20, 2014 at 11:49

6 Answers 6

2

The below perl code would replace all the , present inside [] braces with | symbol.

$ perl -pe 's/,(?=[^\[\]]*\])/|/g' file "platform": "iPhone","report_attrs": {"SKU": "XYZ123","Unit Price": "32.25","Qty": "3"},"screen_flow":["Main"|"Login"] "platform": "android","report_attrs": {"SKU": "XYZ123","Unit Price": "32.25","Qty": "3"} 

To save the changes made,

perl -i -pe 's/,(?=[^\[\]]*\])/|/g' file 
Sign up to request clarification or add additional context in comments.

Comments

1

Here is an awk

awk -F"[][]" 'NF>1 {gsub(/,/,"*",$2);$2="["$2"]";sub(/ \[/,"[")}1' file "platform": "iPhone","report_attrs": {"SKU": "XYZ123","Unit Price": "32.25","Qty": "3"},"screen_flow":["Main"*"Login"] "platform": "android","report_attrs": {"SKU": "XYZ123","Unit Price": "32.25","Qty": "3"} 

Comments

1
sed ':a s/\(\[[^][]*\),\([^][]*]\)/\1|\2/ t a' YourFile 

posix version (so --posix with gnu sed) take into accound more than a simple couple between [] and several [] on same line. Need for this e recursif call to change each single occurence one by one until there are no more case.

1 Comment

no back reference like in perl but there are a kind of if/goto that could do the trick with recursive call. Sed have some a logical view of litteral object that is sometime a bite "coming from mars" ;-)
0

my sed solution:

$ sed 's/[[]\(.\+\),\(.\+\)[]]/[\1|\2]/g' file "platform": "iPhone","report_attrs": {"SKU": "XYZ123","Unit Price": "32.25","Qty": "3"},"screen_flow":["Main"|"Login"] "platform": "android","report_attrs": {"SKU": "XYZ123","Unit Price": "32.25","Qty": "3"} 

1 Comment

work for sample request, but limited to 1 [] and max 2 field in []. According that, it's fine
0

The next perl:

perl -pE 's/\[(.*?),(.*?)\]/[\1|\2]/g' <<EOF "platform": "iPhone","report_attrs": {"SKU": "XYZ123","Unit Price": "32.25","Qty": "3"},"screen_flow":["Main","Login"] "platform": "android","report_attrs": {"SKU": "XYZ123","Unit Price": "32.25","Qty": "3"} EOF 

prints:

"platform": "iPhone","report_attrs": {"SKU": "XYZ123","Unit Price": "32.25","Qty": "3"},"screen_flow":["Main"|"Login"] "platform": "android","report_attrs": {"SKU": "XYZ123","Unit Price": "32.25","Qty": "3"} 

2 Comments

it won't work if the input contains ["Main",,"Login"]
@AvinashRaj, sure ;) it is possible to construct many variations, when it will not works.. E.g. for example, when the last ] missing and so on.. :) or when the comma is missing ... ;)
0

This is just yet another instance of the issue discussed at https://stackoverflow.com/a/25350541/1745001.

This should do it:

$ awk -F'[][]' -v OFS= 'gsub(/,/,"|",$2){$2="["$2"]"}1' file "platform": "iPhone","report_attrs": {"SKU": "XYZ123","Unit Price": "32.25","Qty": "3"},"screen_flow":["Main"|"Login"] "platform": "android","report_attrs": {"SKU": "XYZ123","Unit Price": "32.25","Qty": "3"} 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.