Link to home
Start Free TrialLog in
Avatar of chandru_sol
chandru_solFlag for India

asked on

Powershell to edit multiple strings in text file

Hi,

I am looking for a powershell script to edit multiple lines in a text file where value of the string is with in quotes "value"

Save changes with value_filename

Is this something that can be done to change multiple strings/lines values in txt file using powershell

Thanks
Avatar of coraxal
coraxal
Flag of United States of America image

Can you provide an example of what you're trying to do?
Avatar of chandru_sol

ASKER

for example i have a file with below entry

deviceManagement[1]["securePort"] = "443"
deviceManagement[2]["ifName"] = "bdg2"
radiusAttributes[1]["nasIdentifier"] = "100"

to
deviceManagement[1]["securePort"] = "80"
deviceManagement[2]["ifName"] = "dgp2"
radiusAttributes[1]["nasIdentifier"] = "25"

There will be 50 + lines for which changes needs to be done either input or from text file

hope it is clear
Avatar of Meir Rivkin
create txt file with all changes need to be applies, for example:

securePort 80
nasIdentifier 25

the powershell script will look for all entries ["securePort"] and change value to "80"
same thing goes to nasIdentifier.

is that would be ok?
SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel 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
There is something missing in line 9, $value is never set, and I cannot see how you should do that.
Another, similar way:
$map = import-csv C:\Temp\Values.txt -delimiter '  ' -header key, value

get-content c:\temp\source.txt | foreach-object {
   $line = $_
   foreach ($cur in $map)
   {  $line = $line -replace "(.+\[""$($cur.key)""\] =).*", "`$1 $($cur.value)"   }
  $line
} | out-File C:\Temp\Output.txt

Open in new window

Line 9 should be:
$value = $map[$_]
Thanks Guys!!

File extension is .cfg. is it ok to open .cfg in powershell and save it to .cfg without any changes in foramt

KC
ASKER CERTIFIED SOLUTION
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
Thanks! How to find format of existing file?