[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

file processing question

Hi

i have a file that looks like this


{
      clusters: {
      "as558.mail.vip.ne1.yahoo.com": {
       hosts: [
          "as55801.mail.ne1.com",
          "as55802.mail.ne1.com",
          "as55803.mail.ne1.com",
          "as55804.mail.ne1.com"
       ]
     },

      },
      map: {
      FIXME: {
       lightFetchOn: false,
       lightYCA: "",
       light: "NONE"
    },
    "323": {
       lightFetchOn: true,
       lightYCA: "ya",
       light: "ls323.mal.com:4080"
    },
    "318": {
       lightFetchOn: true,
       lightYCA: "ya",
       light: "ls318.mal.com:4080"
    }
  }
      
}
      
      
      
      I want to add three lines with commas on after lightFetchOn like
      
      mynew1: "string",
    mynew2: 0,
    mynew3: 400,

see the output file below. teh basic algorithm is

1) look for map key wihin the clusters key,
2) find teh first key lightFetchOn
3) below that line add 3 new keys described above

      
      so that my file looks like
      
      {
            clusters: {
            "as558.mail.vip.ne1.yahoo.com": {
             hosts: [
                "as55801.mail.ne1.com",
                "as55802.mail.ne1.com",
                "as55803.mail.ne1.com",
                "as55804.mail.ne1.com"
             ]
           },

            },
            map: {
            FIXME: {
             lightFetchOn: false,
             lightYCA: "",
             light: "NONE"
          },
          "323": {
             lightFetchOn: true,
             mynew1: "string",
             mynew2: 0,
             mynew3: 400,
             lightYCA: "ya",
             light: "ls323.mal.com:4080"
          },
          "318": {
             lightFetchOn: true,
             lightYCA: "ya",
             light: "ls318.mal.com:4080"
          }
        }

      }
      
      can i do it programmatically? and preserve tehe  formatting
{
	clusters: {
	"as558.mail.com": {
       hosts: [
          "as55801.mail.ne1.com",
          "as55802.mail.ne1.com",
          "as55803.mail.ne1.com",
          "as55804.mail.ne1.com"
       ]
     },

	},
	map: {
	FIXME: {
       lightFetchOn: false,
       lightYCA: "",
       light: "NONE"
    },
    "323": {
       lightFetchOn: true,
       lightYCA: "ya",
       light: "ls323.mal.com:4080"
    },
    "318": {
       lightFetchOn: true,
       lightYCA: "ya",
       light: "ls318.mal.com:4080"
    }
  }
	
}
	
	
	
	I want to add three lines with commas on after lightFetchOn like
	
	mynew1: "string",
    mynew2: 0,
    mynew3: 400,

see the output file below. teh basic algorithm is

1) look for map key wihin the clusters key, 
2) find teh first key lightFetchOn
3) below that line add 3 new keys described above

	
	so that my file looks like
	
	{
		clusters: {
		"as558.mail.vip.ne1.yahoo.com": {
	       hosts: [
	          "as55801.mail.ne1.com",
	          "as55802.mail.ne1.com",
	          "as55803.mail.ne1.com",
	          "as55804.mail.ne1.com"
	       ]
	     },

		},
		map: {
		FIXME: {
	       lightFetchOn: false,
	       lightYCA: "",
	       light: "NONE"
	    },
	    "323": {
	       lightFetchOn: true,
	       mynew1: "string",
	       mynew2: 0,
	       mynew3: 400,
	       lightYCA: "ya",
	       light: "ls323.mal.com:4080"
	    },
	    "318": {
	       lightFetchOn: true,
	       lightYCA: "ya",
	       light: "ls318.mal.com:4080"
	    }
	  }

	}
	
	can i do it programmatically?

Open in new window

0
Vlearns
Asked:
Vlearns
  • 3
  • 3
  • 2
1 Solution
 
magentoCommented:
under 318 you dont need these 3 lines?
0
 
ozoCommented:
while( <DATA> ){
    print;
    print qq($1mynew1: "string",\n$1mynew2: 0,\n$1mynew3: 400,\n) if ?(\s*)lightFetchOn: true?
}
__DATA__
{
      clusters: {
      "as558.mail.com": {
       hosts: [
          "as55801.mail.ne1.com",
          "as55802.mail.ne1.com",
          "as55803.mail.ne1.com",
          "as55804.mail.ne1.com"
       ]
     },

      },
      map: {
      FIXME: {
       lightFetchOn: false,
       lightYCA: "",
       light: "NONE"
    },
    "323": {
       lightFetchOn: true,
       lightYCA: "ya",
       light: "ls323.mal.com:4080"
    },
    "318": {
       lightFetchOn: true,
       lightYCA: "ya",
       light: "ls318.mal.com:4080"
    }
  }
      
}
0
 
magentoCommented:
Ozo - Your solutions are always great...

Can u please provide some comment on the code..
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ozoCommented:
What do you want to know about the code?
0
 
VlearnsAuthor Commented:
hi ozo,

the file is very huge, i cannot copy paste it inline...can i read the file from the disk as input?

thanks
0
 
VlearnsAuthor Commented:
Hi ozo

only one substitution happens

if there are more than 1 line that needs substitution, how to change the code?

0
 
ozoCommented:
Your example showed only  the first lightFetchOn: true under "323": { being substituted, not the second ightFetchOn: true under "318": { so I used ?? instead of //
If you want all substituted, use //

If you want to read from @ARGV instead of __DATA__, use <> instead of <DATA>
if you have an open file to read, use the name of the filehandle.
0
 
VlearnsAuthor Commented:
thanks ozo!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now