Go Premium for a chance to win a PS4. Enter to Win

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

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
0
chandru_sol
Asked:
chandru_sol
  • 4
  • 3
  • 2
  • +1
2 Solutions
 
coraxalCommented:
Can you provide an example of what you're trying to do?
0
 
chandru_solAuthor Commented:
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
0
 
Meir RivkinFull stack Software EngineerCommented:
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?
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
Meir RivkinFull stack Software EngineerCommented:
so here's the script.
c:\temp\values.txt - contains the values need to be changed in the following format:
securePort 80
nasIdentifier 25

name of the property (without quotes) + space + new value to applied

c:\temp\source.txt - is the source txt file with the lines to be edited.
c:\temp\output.txt - is the output file (you can overwrite the source file if you wish).

$map = @{}
gc c:\temp\values.txt | %{$map.Add($_.split(' ')[0], $_.split(' ')[1])}

$output=''
gc c:\temp\source.txt | %{
	$line = $_
	$map.Keys | %{
		if($line.contains($_)){
			$value = 
			$line = ($line.split('=')[0] + "=`"$value`"") 
		}
	}
	
	$output+=$line + [Environment]::NewLine
}

$output | Out-File c:\temp\output.txt

Open in new window

0
 
QlemoC++ DeveloperCommented:
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

0
 
Meir RivkinFull stack Software EngineerCommented:
Line 9 should be:
$value = $map[$_]
0
 
chandru_solAuthor Commented:
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
0
 
QlemoC++ DeveloperCommented:
Not without changes. We do not know the original format (ASCII, UTF8, UTF16) of the file, and out-file will use UTF16 (Unicode) by default. And using the direct pipe solution I showed will not work.

Assuming you have "standard" ASCII files:
$map = import-csv C:\Temp\Values.txt -delimiter '  ' -header key, value

$text = get-content c:\temp\File.Cfg
foreach ($line in $text)
   foreach ($cur in $map)
   {  $line = $line -replace "(.+\[""$($cur.key)""\] =).*", "`$1 $($cur.value)"   }
  $line
} | out-File -Encoding ASCII C:\Temp\File.Cfg

Open in new window

0
 
chandru_solAuthor Commented:
Thanks! How to find format of existing file?
0
 
Meir RivkinFull stack Software EngineerCommented:
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

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