Solved

Powershell to edit multiple strings in text file

Posted on 2013-01-28
10
980 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 70

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 70

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

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.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

752 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