Link to home
Start Free TrialLog in
Avatar of Vlearns
Vlearns

asked on

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

Avatar of magento
magento

under 318 you dont need these 3 lines?
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"
    }
  }
      
}
Ozo - Your solutions are always great...

Can u please provide some comment on the code..
What do you want to know about the code?
Avatar of Vlearns

ASKER

hi ozo,

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

thanks
Avatar of Vlearns

ASKER

Hi ozo

only one substitution happens

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

ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Vlearns

ASKER

thanks ozo!