Solved

Powershell to edit multiple strings in text file

Posted on 2013-01-28
10
942 Views
Last Modified: 2013-04-17
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
Comment
Question by:chandru_sol
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 5

Expert Comment

by:coraxal
ID: 38829999
Can you provide an example of what you're trying to do?
0
 
LVL 12

Author Comment

by:chandru_sol
ID: 38830021
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
 
LVL 42

Expert Comment

by:sedgwick
ID: 38830115
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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 42

Assisted Solution

by:sedgwick
sedgwick earned 250 total points
ID: 38830170
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
 
LVL 69

Expert Comment

by:Qlemo
ID: 38860845
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
 
LVL 42

Expert Comment

by:sedgwick
ID: 38860961
Line 9 should be:
$value = $map[$_]
0
 
LVL 12

Author Comment

by:chandru_sol
ID: 38864539
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
 
LVL 69

Accepted Solution

by:
Qlemo earned 250 total points
ID: 38864721
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
 
LVL 12

Author Comment

by:chandru_sol
ID: 38866358
Thanks! How to find format of existing file?
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38866941
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question