Home > Software design >  Find and replace using wildcard in vi editor
Find and replace using wildcard in vi editor

Time:05-16

I have a large text file that resembles json format. Part of the text is something like this:

...

{
"term": 
"to_zone" : ["sas"]
"from_zone" : ["sas-inside inside"]
"source" : ["10.100.31.111/32 10.100.31.112/32 10.100.31.113/32 100.100.5.31/32 100.100.5.32/32 100.100.5.33/32"]
"destination": ["100.100.8.129/32 100.100.8.130/32 100.100.8.131/32"]
"source_user" : ["any"]
"category" : ["any"]
"application" : ["any"]
"service" : ["tcp-2181 tcp-10500"]
"source_hip" : ["any"]
"destination_hip" : ["any"]
"tag" : ["sas"]
"action" : ["allow"]
"profile_setting" : ["alert-only"]
},

Is there any way possible in vi editor to scan through the 'lists' and insert the quotations and commas to complete the elements?

The expected result would be:

{
"term": 
"to_zone" : ["sas"]
"from_zone" : ["sas-inside inside"]
"source" : ["10.100.31.111/32","10.100.31.112/32","10.100.31.113/32","100.100.5.31/32","100.100.5.32/32","100.100.5.33/32"]
"destination": ["100.100.8.129/32","100.100.8.130/32","100.100.8.131/32"]
"source_user" : ["any"]
"category" : ["any"]
"application" : ["any"]
"service" : ["tcp-2181","tcp-10500"]
"source_hip" : ["any"]
"destination_hip" : ["any"]
"tag" : ["sas"]
"action" : ["allow"]
"profile_setting" : ["alert-only"]
},

I don't know how the regex would be in the find and replace function in vi editor. Note that there are existing quotations in the beginning and the end of the list, thus making it a one element string, list.

CodePudding user response:

vi has a search and replace function. You would have to run this multiple times until it couldn't find the pattern any longer, but this should do it.

:%s/\(\[".\{-\}\) /\1","/g
  • Related